users@jaxb.java.net

Re: Ummarshalling to Object type

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Sun, 2 May 2010 16:00:29 +0200

You could use a class resulting from the following XML schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           version="2.0">
  <xs:element name="doc" type="DocType"/>
  <xs:complexType name="DocType">
    <xs:sequence>
      <xs:element name="field" type="xs:anySimpleType"/>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string"/>
  </xs:complexType>
</xs:schema>

Setting the Object field to an Integer and marshalling results in
  <doc name="integer">
      <field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:type="xs:int">42</field>
  </doc>
and this unmarshals properly.

I don't think it's possible to have also an attribute in the element
which has anySimpleType as its @XmlSchemaType. For this the XML Schema
type would have to be a complex type.

-W



On Sun, May 2, 2010 at 9:49 AM, zeiss <zeiss_at_tut.by> wrote:
>
> Hi,
>
> I need to unmarshal XML element value to a field of type Object. I also
> would like JAXB implementation to automatically handle type information I
> provide in XML with xsi:type attribute. The result would be an object of
> corresponding type as defined by XML data types to Java classes mapping.
>
> Unfortunately, JAXB implementation that I am using (JAXB RI 2.2) seems to
> ignore xsi:type in my case. It always creates an object of type String. I am
> wondering if I can get the desired result at all when using xsi:type
> information.
>
> Here is a simple sample:
>
> <field name="field1" xsi:type="xs:integer"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xs="http://www.w3.org/2001/XMLSchema">1</field>
>
> @XmlRootElement(name = "field")
> public class Field {
>
>  private String name;
>
>  private Object value;
>
>  @XmlAttribute(name = "name")
>  public String getName() {
>    return name;
>  }
>
>  public void setName(String name) {
>    this.name = name;
>  }
>
>  @XmlValue
>  @XmlSchemaType(name = "anySimpleType")
>  public Object getValue() {
>    return value;
>  }
>
>  public void setValue(Object value) {
>    this.value = value;
>  }
> }
> --
> View this message in context: http://old.nabble.com/Ummarshalling-to-Object-type-tp28425349p28425349.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
>
>