users@jaxb.java.net

Re: Problem marshaling <xs:any> element with attributes

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 3 Jun 2009 11:45:22 +0200

I'm posting my comment to JAXB issue 647 so that it's on this list, too.

Shedding some more light on this issue:

- using elementFormDefault="unqualified" in the schema => WORKS

- defining and using a prefix in the inserted XML => WORKS
 <xx:someData xmlns:xx=\"http://www.company.com/Product/Component/v1.0\">
 <xx:data1 name=\"one\">
   <xx:data2>1234</xx:data2>
   <xx:data3>1234</xx:data3>
 </xx:data1>
</xx:someData

- using "qualified" for both elements and attributes AND everything prefixed in
 the inserted XML => WORKS
 <xx:someData xmlns:xx=\"http://www.company.com/Product/Component/v1.0\">
 <xx:data1 xx:name=\"one\">
   <xx:data2>1234</xx:data2>
   <xx:data3>1234</xx:data3>
 </xx:data1>
</xx:someData

Notice that the original combination specifies qualified elements for
the document but the inserted DOM tree (which is not generated
by JAXB) does not conform to this specification. I am guessing (!)
that the underlying XML writer is confused by this deviation, and it
may very well be that this confusion only arises from the presence
of an attribute node. If my guess is correct, the problem would have
to be fixed in the apache/xerces XML writer. OTOH, the posted
usage may be coinsidered incorrect.

-W


On Wed, May 27, 2009 at 1:36 PM, Anand Navale <naanand_at_novell.com> wrote:
> Hi,
>
> The problem gets solved when I use JAXB RI 2.0.5 instead of JAXB RI 2.1.7.  I tried the latest JAXB RI version 2.1.11 but problem still persisted. Then on a bit of googling, came across this post http://forums.java.net/jive/thread.jspa?messageID=330026  which talks about a similar issue for which there is a bug filed on JAXB RI. Refer https://jaxb.dev.java.net/issues/show_bug.cgi?id=585
>
> Is there any other solution to this problem than moving from 2.1.x to 2.0.5?
>
> Thanks
> Anand Navale.
>
>
>
>>>> On 5/25/2009 at 7:05 PM, in message <4A1D707C.456A.0095.0_at_novell.com>, Anand
> Navale wrote:
>> Hi,
>>
>> Have observed that this problem occurs when the following is true:
>> 1) namespace of the object to be marshalled is same as DOM element mapping
>> the <xs:any> element
>> 2) the DOM element namespace is its default namespace, i.e., there is no
>> namepace prefix for this namespace. Sample DOM element XML:
>>     <Data1 Id="Element data id"
>> xmlns="http://www.company.com/Product/Component/v1.0">
>>       <Name>Element data name</Name>
>>       <Type>Element data type</Type>
>>     </Data1>
>>
>> Have seen that there are no issues if anyone of the above is not true.
>>
>> Hope this is helpful in resolving the problem.
>>
>> Thanks
>> Anand Navale.
>>
>>
>>
>> >>> On 5/25/2009 at 5:04 PM, in message <4A1AEA32.456A.0095.0_at_novell.com>, Anand
>> Navale wrote:
>> > Hi,
>> >
>> > This is in context of the thread
>> > https://jaxb.dev.java.net/servlets/BrowseList?list=users&by=thread&fr
>> > om=1765537
>> > The link to the original post :
>> > https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=8862
>> >
>> > Have noticed that this problem occurs only when the namespace of the object
>> > to be marshalled is same as DOM element mapping the <xs:any> element. But I
>> > do not have the luxury of changing the namespace of that element. Any ways
>> to
>> > get around this problem?
>> >
>> > Appreciate your support.
>> >
>> > Thanks in advance,
>> > Anand Navale.
>> >
>> >
>> > >>> On 5/15/2009 at 3:22 PM, in message <4A1ACDA4.456A.0095.0_at_novell.com>, Anand
>> > Navale wrote:
>> > > Hi,
>> > >
>> > > Here's the schema:
>> > > <?xml version="1.0" encoding="UTF-8"?>
>> > > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
>> > >   xmlns="http://www.company.com/Product/Component/v1.0"
>> > >
>> > > targetNamespace="http://www.company.com/Product/Component/v1.0"
>> > >   elementFormDefault="qualified" attributeFormDefault="unqualified">
>> > >   <xs:complexType name="Element">
>> > >           <xs:sequence>
>> > >                   <xs:element name="Name" type="xs:string" minOccurs="0" />
>> > >                   <xs:element name="Description" type="xs:string" minOccurs="0" />
>> > >                   <xs:element name="Data" minOccurs="0">
>> > >                           <xs:complexType>
>> > >                                   <xs:sequence>
>> > >                                           <xs:any/>
>> > >                                   </xs:sequence>
>> > >                           </xs:complexType>
>> > >                   </xs:element>
>> > >           </xs:sequence>
>> > >           <xs:attribute name="Type" type="xs:string" />
>> > >           <xs:attribute name="Id" type="xs:string" use="required" />
>> > >   </xs:complexType>
>> > >   <xs:complexType name="ElementSet">
>> > >           <xs:sequence>
>> > >                   <xs:element name="Element" type="Element" minOccurs="0"
>> > > maxOccurs="unbounded"/>
>> > >                   <xs:element name="ElementSetAny" minOccurs="0">
>> > >                           <xs:complexType>
>> > >                                   <xs:sequence>
>> > >                                           <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
>> > >                                   </xs:sequence>
>> > >                           </xs:complexType>
>> > >                   </xs:element>
>> > >           </xs:sequence>
>> > >           <xs:attribute name="Type" type="xs:string"/>
>> > >           <xs:attribute name="Version" type="xs:long" use="optional"/>
>> > >   </xs:complexType>
>> > > </xs:schema> <!-- schema ends here -->
>> > >
>> > > Appreciate your support.
>> > >
>> > > FYI, the entire code along with the schema is part of the original post.
>> > >
>> > > Thanks,
>> > > Anand Navale.
>> > >
>> > > >>> On 5/15/2009 at 2:25 PM, in message
>> > > <3399eb530905150155p6fa941bdh8d4dc0494095a110_at_mail.gmail.com>, Aleksei Valikov
>> > > <valikov_at_gmx.net> wrote:
>> > > > Hi,
>> > > >
>> > > >> Also including the method getDOMFromXML()....
>> > > >>
>> > > >>        Document getDOMfromXML(Reader reader) throws Exception{
>> > > >>                DocumentBuilderFactory factory =
>> > > > DocumentBuilderFactory.newInstance();
>> > > >>                DocumentBuilder builder = factory.newDocumentBuilder();
>> > > >>                InputSource is = new InputSource(reader);
>> > > >>                Document document = builder.parse(is);
>> > > >>                return document;
>> > > >>        }
>> > > >>
>> > > >> Here's the method that creates the ElementSet object and marshals it.
>> > > >>
>> > > >>
>> > > >> public void testJaxbProcessing() throws Exception {
>> > > >>        ElementSet es = new ElementSet();
>> > > >>        es.setType("es1");
>> > > >>        es.setVersion(Long.valueOf(0L));
>> > > >>
>> > > >>        Element e = new Element();
>> > > >>        e.setName("e1");
>> > > >>        e.setType("e_type1");
>> > > >>        e.setId("1");
>> > > >>        Data data = new Data();
>> > > >>
>> > > >>        Document anyValue = getDOMfromXML(new StringReader("<someData
>> > > >>        xmlns=\"http://www.company.com/Product/Component/v1.0\"><data1
>> > > >>
>> > > name=\"one\"><data2>1234</data2><data3>1234</data3></data1></someData>"));
>> > > >>
>> > > >>        data.setAny(anyValue.getDocumentElement());
>> > > >>        e.setData(data);
>> > > >>        es.getElement().add(e);
>> > > >>        JAXBElement<ElementSet> jaxbElem = new JAXBElement<ElementSet>(
>> > > >>        new QName("", ElementSet.class.getSimpleName()),
>> > > >>        ElementSet.class, null, es);
>> > > >>
>> > > >>        // MARSHALLING
>> > > >>        String esString = marshal(jaxbElem,
>> > > > ElementSet.class.getPackage().getName(), ElementSet.class.getClassLoader());
>> > > >>
>> > > >>        System.out.println(esString);
>> > > >> }
>> > > >
>> > > >
>> > > > Your code seems to be allright.
>> > > > Would you also send me the schema files? I'll try it when I have time
>> > > > over the weekend.
>> > > >
>> > > > Bye.
>> > > > /lexi
>> > >
>> > >
>> > >
>> >
>> >
>> >
>>
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>