[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 10:30:18 EST 2009


Hi Laura,

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

> I'm assuming that means that I haven't formatted the DOMDocument 
> correctly. I've tried several things, but I keep getting the error. I'm 
> sure it's something stupid. Here is a code fragment for creating the 
> document and commandEcho:
> 
>       DOMDocument *cmd_doc = impl->createDocument(xml::string 
> ("<XYZ>").c_str(),
>                                                   xml::string 
> ("XYZ:CommandA").c_str(), NULL);
>       auto_ptr<commandEcho> ec(new commandEcho 
> (*cmd_doc->getDocumentElement(), 0 , NULL));
>       cmd_doc->release();
>       DOMElement& e = ec->any();
>       e << *lchc;

Ok, I know what's going on. Unfortunately when a type has a single
required (minOccurs=maxOccurs=1) wildcard, the calls to the normal 
constructor and the parsing constructor are ambiguous (this problem
is already in our defect list). The above code calls the parsing
constructor and there is no way to call the other one, which is
what we need.

We can work around this problem like so:

1. Recompile the schema containing the commandEcho type with the
   --generate-default-ctor option.

2. Then the above code can be rewritten like this (it is actually
   simpler and more efficient):

auto_ptr<commandEcho> ec (new commandEcho);

DOMDocument& doc = ec->dom_document ();
DOMElement* e = doc.createElementNS (
  xml::string ("<XYZ>").c_str (), 
  xml::string ("XYZ:CommandA").c_str ());

*e << *lchc;

ec->any (e); // Assumes ownership of e.

Here, we create an element using the DOM document that holds DOM
fragments for the wildcard. Because of that, we can let the
commandEcho object simply adopt ownership of the element once we
are done serializing the command into it.

Let me know if there are any problems with this code.

Boris




More information about the xsd-users mailing list