I think I have confirmed my previous statement with an experiment,
using your complexType "field" (which I renamed "FieldType"), an XML
data file containing element <field2> of that type:
<field2 name="field"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="
http://www.w3.org/2001/XMLSchema"
xsi:type="xs:int">123</field2>
Then I called unmarshal, with validation, using the XML schema. This
is what I get:
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.4.3: Type 'xs:int' is not
validly derived from the type definition, 'FieldType', of element
'field2'.]
At the bottom of this is:
Caused by: org.xml.sax.SAXParseException: cvc-elt.4.3: Type 'xs:int'
is not validly derived from the type definition, 'FieldType', of
element 'field2'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
[snip]
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:413)
So the xerces XML Schema validator seems to confirm that there is a conflict.
-W
On Mon, May 3, 2010 at 2:24 PM, zeiss <zeiss_at_tut.by> wrote:
>
> The corresponding schema is as follows:
>
> <xs:complexType name="field">
> <xs:simpleContent>
> <xs:extension base="xs:anySimpleType">
> <xs:attribute type="xs:string" name="name"/>
> </xs:extension>
> </xs:simpleContent>
> </xs:complexType>
>
> What I understood from your answer is that I have to create a child element
> for <field> to hold element value. There is no other way around it if I want
> JAXB to unmarshal anySimpleType to required Java type.
>
> Now I am wondering if the impossibility to use @XmlValue here is a
> limitation of the current JAXB specification or there is any technical
> reason for this.
>
> Anyway, thanks for your guidance.
>
>
> Wolfgang Laun-2 wrote:
>>
>> 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
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> 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-tp28425349p28434520.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
>
>