[xsd-users] Serializing just the contents of an "any" element

Iain Sharp isharp at atis.org
Tue Jun 20 03:47:52 EDT 2017


Thanks Boris,

That is helpful. Your point about multiple root elements is a good one. As far as I can tell in reality, and certainly for the application I am working on, there is only ever one root element populated in the any wildcard.

I suspected that I may have to work through the DOM using the techniques from the FAQ. At first glance this looks rather low-level and a little intimidating so I wanted to check that I wasn't missing a simpler, higher level approach before looking in detail at the DOM option. I am surprised that the code generator doesn't create a generic set of serialization methods for the ultimate base class of all the XML element classes. 


Regards

Iain


-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com] 
Sent: 19 June 2017 18:20
To: Iain Sharp <isharp at atis.org>
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsd-users] Serializing just the contents of an "any" element

Iain Sharp <isharp at atis.org> writes:

> <xs:complexType name="primitiveContent">
>   <xs:choice minOccurs="0" maxOccurs="unbounded">
>     <xs:any namespace="http://www.onem2m.org/xml/protocols" processContents="lax" />
>     <xs:any namespace="##other" processContents="lax"  />
>   </xs:choice>
> </xs:complexType>
>
> [...]
>
> <m2m: primitiveContent xmlns:m2m="http://www.onem2m.org/xml/protocols">
>   <m2m:ae rn="myAE" xmlns:m2m="http://www.onem2m.org/xml/protocols">
>     <api>myAE</api>
>     <aei>myAE</aei>
>     <rr>true</rr>
>   </m2m:ae>
> </m2m: primitiveContent >
> 
> What I want is:
>
>   <m2m:ae rn="myAE" xmlns:m2m="http://www.onem2m.org/xml/protocols">
>     <api>myAE</api>
>     <aei>myAE</aei>
>     <rr>true</rr>
>   </m2m:ae>

Your schema allows for multiple elements in the wildcard content. That is, your could have something like this:

<m2m: primitiveContent xmlns:m2m="http://www.onem2m.org/xml/protocols">
  <m2m:ae xmlns:m2m="http://www.onem2m.org/xml/protocols">
    ...
  </m2m:ae>
  <m2m:ae xmlns:m2m="http://www.onem2m.org/xml/protocols">
    ...
  </m2m:ae>
  <m2m:ae xmlns:m2m="http://www.onem2m.org/xml/protocols">
    ...
  </m2m:ae>
</m2m:primitiveContent >

I am not sure what you want to do with that. Serializing them all will create a "multi-root" document which is not a valid XML.

In any case, what you need to do is serialize primitiveContent to DOM.
Then you can iterate over its child elements and serialize them one at a time. Or, alternatively, you could iterate over the wildcard content and just serialize each element -- will probably be more efficient. Examples and the FAQ on the Wiki have some sample code for doing this.

Boris



More information about the xsd-users mailing list