users@jaxb.java.net

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

From: Anand Navale <naanand_at_novell.com>
Date: Mon, 25 May 2009 07:35:11 -0600

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
> >
> >
> >
>
>
>