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

Laura E Fowler Laura_E_Fowler at raytheon.com
Fri Mar 6 11:54:04 EST 2009


Boris,

I'm getting closer, but when I execute following line:

*e << *lchc;

I receive the following exception:
terminate called after throwing an instance of 
'xsd::cxx::xml::dom::no_prefix'

I saw in the archives where there had been a problem with no_prefix, but I 
wasn't sure if this was the same case.

Thanks, 
Laura Fowler


 



From:
Boris Kolpackov <boris at codesynthesis.com>
To:
Laura E Fowler <Laura_E_Fowler at raytheon.com>
Cc:
xsd-users at codesynthesis.com
Date:
03/06/2009 09:38 AM
Subject:
Re: [xsd-users] How to clone/copy an XML message and add it to a another 
message using anyType



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