Below is the generated Java code. You should be able to modify your
code according to this.
The problem is that you cannot use @XmlValue, i.e., the anySimpleType
must be in a proper XML element of its own, i.e., a child of DocType
(or your Field).
As a rule, if you can provide an XML schema that describes what you
want, it should be possible to hand-code Java accordingly. If you
cannot come up with an XML schema, all bets are off ;-)
-W
public class DocType {
@XmlElement(required = true)
@XmlSchemaType(name = "anySimpleType")
protected Object anyValue;
@XmlAttribute
protected String name;
public Object getAnyValue() {
return field;
}
public void setAnyValue(Object value) {
this.anyValue = value;
}
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
}
On Mon, May 3, 2010 at 11:32 AM, zeiss <zeiss_at_tut.by> wrote:
>
> Hi Wolfgang,
>
> The problem is that I want to use my own class to hold value of XML simple
> type. The JAXB 2.2 spec says that xs:anySimpleType is mapped to both
> java.lang.Object and java.lang.String. It is not completely clear to me how
> JAXB implementation should choose String over Object and vice versa.
>
> I would expect that xsi:type controls this so that if concrete type like
> xs:int specified, then the value is of Java int type (probably boxed in
> java.lang.Integer). However, the behavior that I see is that the type is
> always java.lang.String. When I specify something like
>
> @XmlValue
> @XmlSchemaType(name = "anySimpleType")
> protected Object value
>
> I expect JAXB implementation to perform type substitution based on the
> xsi:type attribute.
>
> I am wondering if this area is not covered by JAXB so I cannot expect type
> substitution to work for the case above.
>
>
> Wolfgang Laun-2 wrote:
>>
>> 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
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>>
>>
>
> --
> View this message in context: http://old.nabble.com/Ummarshalling-to-Object-type-tp28425349p28433140.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
>
>