users@jaxb.java.net

Re: No values in JAXBElement wrapped class

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 16 Mar 2011 19:46:37 +0100

I don't quite believe this snippet:

    String tag = ((JAXBElement)s).getName().getLocalPart();
    if (s instanceof EnFamily) {
           EnFamily chunk = (EnFamily)((JAXBElement) s).getValue();

If s is of type EnFamily it cannot be cast to JAXBElement. I'd expect a test
comparing the tag String to "family".

-W


On 16 March 2011 15:11, <eaudet_at_jarics.com> wrote:

> Hello,
>
> I have generated my java classes using this command:
>
> xjc -d ./src MySchema.xsd
>
>
> Everything compiles. Nice!. Here's a part of my xsd in problem:
>
> ...
> <complexType name="EN">
> <complexContent>
> <extension base="{urn:hl7-org:v3}ANY">
> <sequence>
> <choice maxOccurs="unbounded">
> <element name="delimiter"
> type="{urn:hl7-org:v3}en.delimiter"/>
> <element name="family" type="{urn:hl7-org:v3}en.family"/>
> <element name="given" type="{urn:hl7-org:v3}en.given"/>
> <element name="prefix" type="{urn:hl7-org:v3}en.prefix"/>
> <element name="suffix" type="{urn:hl7-org:v3}en.suffix"/>
> </choice>
> <element name="validTime" type="{urn:hl7-org:v3}IVL_TS"
> minOccurs="0"/>
> </sequence>
> <attribute name="use" type="{urn:hl7-org:v3}set_EntityNameUse"
> />
> </extension>
> </complexContent>
> </complexType>
> .....
>
>
>
> Here's the generated **EN** class:
>
> ....
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "EN", propOrder = {
> "content"
> })
> @XmlSeeAlso({
> ON.class,
> PN.class,
> TN.class
> })
> public class EN {
>
> @XmlElementRefs({
> @XmlElementRef(name = "delimiter", namespace =
> "urn:hl7-org:v3", type = JAXBElement.class),
> @XmlElementRef(name = "validTime", namespace =
> "urn:hl7-org:v3", type = JAXBElement.class),
> @XmlElementRef(name = "suffix", namespace = "urn:hl7-org:v3",
> type = JAXBElement.class),
> @XmlElementRef(name = "prefix", namespace = "urn:hl7-org:v3",
> type = JAXBElement.class),
> @XmlElementRef(name = "family", namespace = "urn:hl7-org:v3",
> type = JAXBElement.class),
> @XmlElementRef(name = "given", namespace = "urn:hl7-org:v3",
> type = JAXBElement.class)
> })
> @XmlMixed
> protected List<Serializable> content;
> ......
>
>
>
> Here's the related XML example file in problem (fits with the above
> XSD) :
>
>
>
> ...
> <person.name>
> <value>
> <family>Smith</family>
> <given>John</given>
> </value>
> </person.name>
> ...
>
>
>
>
>
> Here's the java code doing the unmarshalling in problem:
>
>
>
>
> ...
> for (Serializable s :
> pPersonName.getValue().getContent()) {
> if (s instanceof String) {
> System.out.print((String) s);
> } else {
> String tag = ((JAXBElement)
> s).getName().getLocalPart();
> if (s instanceof EnFamily) {
> EnFamily chunk = (EnFamily)
> ((JAXBElement) s).getValue();
> System.out.print("(" + tag +
> ":");
> }
> }
> }
> ...
>
>
>
> And here's my problem: The class **EnFamily** extracted from a
> JAXBElement does not contain any values. It should contain "Smith". It
> seems it is not unmarshalling correctly.
>
> Any help appreciated!
>
> - Erick
>