[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 10:12:59 EST 2009


Boris,

I'm trying to use the more efficient way, but when I create the 
commandEcho I get an exception at run time:
Exception received: expected element '##targetNamespace#*'

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;

If I don't put the three parameters on creating the commandEcho the 
compiler says the call is ambiguous. 

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 06:48 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 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