users@jaxb.java.net

Re: Unmarshalling ignores value of anyType element

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 2 Dec 2009 10:05:12 +0100

Similar questions crop up every now and then. That's why I'll
answer this in some detail.

xs:anyType is the "ur-type" (W3C) of all complex schema types. There is a
restriction, xs:anySimpleType, the union of all simple types (and their
xs:list
derivations).

This means that a binding of xs:anyType is not restricted in any way.
XML representations may have to add the type to the element, using, e.g.
<wild xsi:type="xs:string"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Wild</wild>

Here is the corresponding schema using xs:anyType for an element type:
  <xs:element name="docOne" type="DocTypeOne"/>
  <xs:complexType name="DocTypeOne">
    <xs:sequence>
      <xs:element name="text" type="xs:string"/>
      <xs:element name="wild" type="xs:anyType"/>
    </xs:sequence>
  </xs:complexType>

To marshal/unmarshal, simply use setWild( "..." )/getWild()

An explicit complex type definition such as, e.g.,

  <xs:element name="docTwo" type="DocTypeTwo"/>
  <xs:complexType name="DocTypeTwo">
    <xs:sequence>
      <xs:element name="text" type="xs:string"/>
      <xs:element name="wild" type="MyTypeTwo"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="MyTypeTwo">
    <xs:complexContent>
      <xs:extension base="xs:anyType"/>
    </xs:complexContent>
  </xs:complexType>

Results in a Java class MyTypeTwo, which (similar to your type "Varies")
contains field List<Element> any (for all the element(!) children) and
field Map<QName,String> otherAttributes (which might be there even
if not declared in the schema).

But an XML element instance
   <wild>xyz</wild>
doesn't have a child element - it has text content.

At this point, you'll have to decide: Would you prefer to wrap this data
in a subelement for <wild>, or do you modify the schema, defining
the complexType to be mixed?

In the first case, marshalling and unmarshalling becomes complex,
since you must create a org.w3c.dom.Element, from a JAXBElement
defining some tag, and insert this by calling getAny().add( domEl );
and you must use getAny().getTextContent() to retrieve the
subelement's content.

The second case uses this schema definition:

  <xs:complexType name="MyTypeThree">
    <xs:complexContent mixed="1">
      <xs:extension base="xs:anyType"/>
    </xs:complexContent>
  </xs:complexType>

Class MyTypeThree now has List<Object> content, in order to
collect text content chunks and child elements, as they appear
in the element. Full blown processing after unmarshalling would
have to instanceof-test the list elements, and process accordingly.
For simple text content, you can expect a String object, which
can also be put during marshalling.

-W

On Tue, Dec 1, 2009 at 6:55 PM, fcamous <fabrice.camous_at_dit.ie> wrote:

>
> Hi all,
>
> I'm having problem accessing a value contained in an anyType element:
>
> I have this data type called "varies" in my schema:
> <xsd:complexType name="varies">
> <xsd:complexContent>
> <xsd:extension base="xsd:anyType"/>
> </xsd:complexContent>
> </xsd:complexType>
>
> xjc generates Varies.java and I have an getter to access the value:
> public List<Element> getAny() {
> if (any == null) {
> any = new ArrayList<Element>();
> }
> return this.any;
> }
>
> I'm unmarshalling an xml document and this is the part I am interested in:
> <OBX.5>
> 6.4
> </OBX.5>
>
> "OBX.5" is an extension of "varies" type.
> xjc generates OBX5CONTENT.java which inherits the getAny() method. However,
> after unmarshalling, when I use the method in my code, the list of
> org.w3c.dom.Element objects it returns is always empty. Note that I have
> access to other fields of the OBX5CONTENT object without a problem.
> Any idea?
> Thanks in advance.
>
> Here's the OBX.5 schema definition if it's needed:
> <xsd:attributeGroup name="OBX.5.ATTRIBUTES">
> <xsd:attribute name="Item" type="xsd:string" fixed="573"/>
> <xsd:attribute name="Type" type="xsd:string" fixed="varies"/>
> <xsd:attribute name="LongName" type="xsd:string" fixed="Observation
> Value"/>
> </xsd:attributeGroup>
> <xsd:complexType name="OBX.5.CONTENT">
> <xsd:annotation>
> <xsd:documentation xml:lang="en">Observation Value</xsd:documentation>
> <xsd:appinfo>
> <hl7:Item>573</hl7:Item>
> <hl7:Type>varies</hl7:Type>
> <hl7:LongName>Observation Value</hl7:LongName>
> </xsd:appinfo>
> </xsd:annotation>
> <xsd:complexContent>
> <xsd:extension base="varies">
> <xsd:attributeGroup ref="OBX.5.ATTRIBUTES"/>
> </xsd:extension>
> </xsd:complexContent>
> </xsd:complexType>
> <xsd:element name="OBX.5" type="OBX.5.CONTENT"/>
> --
> View this message in context:
> http://old.nabble.com/Unmarshalling-ignores-value-of-anyType-element-tp26595595p26595595.html
> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>