[xsd-users] Serialize ony a specific xml tag

Boris Kolpackov boris at codesynthesis.com
Fri Oct 5 05:29:16 EDT 2007


Hi Michael,

Forstner Michael <Michael.Forstner at cpg.de> writes:

> is there an easy way with XSD Tree Mapping to serialize only one
> specific XML tag instead of the whole document?

There are two ways to do this:

1. If the element in question is defined globally in the schema then
   you can generate serialization functions for this element and use
   them to serialize it to XML. You may also want to pass the
   no_xml_declaration flag in order to get rid of XML declaration
   in the resulting output. The 'streaming' example in the
   examples/cxx/tree/ directory uses this approach.


2. If the element is local or you don't want to use serialization
   functions, then you can create a DOMDocument yourself with
   the desired root element, then serialize the data to DOMElement,
   and finally serialize the DOM document to XML text:

   using namespace xercesc;

   const type& r = ... // element data

   xml::dom::auto_ptr<DOMDocument> doc =
     create ("root",
             "http://www.compnay.com/namespace",
             "my");

   DOMElement* e = doc->getDocumentElement ();

   *e << r;

   serialize (std::cout, *doc);


   In the above code fragment, create() and serialize() functions
   are from the C++/Tree Mapping FAQ questions 3.1 and 3.2:

   http://wiki.codesynthesis.com/Tree/FAQ

   Note also that you need to compile your schemas with the
   --generate-serialization option in order to get operator<< used
   in *e << r;


Boris




More information about the xsd-users mailing list