[xsd-users] How to clone/copy an XML message and add it to a another message using anyType

Boris Kolpackov boris at codesynthesis.com
Fri Mar 6 07:40:30 EST 2009


Hi Laura,

Laura E Fowler <Laura_E_Fowler at raytheon.com> writes:

> I have been looking at this, but I think I'm not understanding the 
> following statement you made:
> "This way you could just serialize one of the commands into a DOM document
> fragment corresponding to the wildcard and then, when the Status message 
> is serialized, everything is handled automatically."
> 
> I'm a little confused by "DOM document fragment corresponding to the 
> wildcard". I've been looking at the wildcard example, but I'm just not 
> getting it.
> 
> My schema is similar to:
> 
> <complexType name="Status">
>   <sequence>
>     ...
>     <xsd:element name="commandEcho" type="xxx:commandEcho"/>
>     ...
>   </sequence>
> </complexType>
> 
>  <xsd:complexType name="commandEcho">
>       <xsd:sequence>
>           <xsd:any namespace="##targetNamespace" processContents="strict"/>
>       </xsd:sequence>
>  </xsd:complexType>
> 
> I serialized my command to a DOMDocument (not a fragment), but I'm unclear 
> as to how put it into commandEcho.

If you compile the schemas with the --generate-wildcard option, the 
above commandEcho type will have a constructor that looks like this:

class commandEcho
{
  commandEcho (const xercesc::DOMElement& any);
};

Here is sample code that shows how to construct the Status object
provided you have a DOMDocument containing the command:

DOMDocument* cmd_doc = ...

auto_ptr<commandEcho> ec (
  new commandEcho (*cmd_doc->getDocumentElement ()));

Status s (ec);

The only problem with this approach is that it is quite wasteful;
you first serialize the command into a DOMDocument and then the
contents of this document are copied into the commandEcho object.
It is also possible to serialize the command directly into a
DOMElement corresponding to the wildcard. Something along these
lines:

const CommandA& cmd_a = ...

// Create empty document with the correct root element.
//
DOMDocument* cmd_doc = 
  impl->createDocument (xml::string ("<command namespace>").c_str ()),
                        xml::string ("xxx:command-a").c_str ());


auto_ptr<commandEcho> ec (
  new commandEcho (*cmd_doc->getDocumentElement ()));

cmd_doc->release (); // Don't need it anymore.

// Serialize directly into wildcard element.
//
DOMElement& e = ec->any ();
e << cmd_a;

Status s (ec);

Boris




More information about the xsd-users mailing list