[xsd-users] Re: Adding namspaces

Matt Burnham mwburn at mhpcc.hpc.mil
Wed Jul 12 22:36:47 EDT 2006


   Boris,
   I understand the concepts of this, but can't get the implementation to
   work.  I've attached a test case that has an "event" schema and
   "embedded" sub-schema.   The code I'm trying to get working is the
   createXML() function in test_xsd.cxx, where I create an event "tree"
   and an "embedded" tree and then need to "add" the embedded tree to the
   detail element of the event.  I'm trying to create the same xml
   "string" that is in the test_event.xml file.
   Thanks,
   Matt
   Boris Kolpackov wrote:

Hi Matt,

Matt Burnham [1]<mwburn at mhpcc.hpc.mil> writes:



   I have one last issue that I need to fix ASAP.  I need to create an
   in-memory tree and then serialize it to XML.

   I have it all working except for the "detail" section.  When I parse this
   section, I use the keep-dom flag, and then load the various sub-schema's
   using the xerces DOMElement's.  So I'm not sure how to "create" the tree
   on the fly.  I'm assuming I need to create new DOMElements and populate
   them, but can't seem to get it working.


The easiest way to achieve this is to (1) serialize event and embedded_data
into two separate DOM documents, then (2) move the embedded_data sub-tree
to the event document, and finally (3) write the resulting DOM document
to XML. The code for these operations will look something like this:

(1):

using namespace xerces-c
namespace xml = xsd:cxx::xml;
namespace dom = [2]xml::dom;

event_type& e = ...
embedded_data_type& ed = ...

dom::auto_ptr<DOMDocument> event_doc (event (e));
dom::auto_ptr<DOMDocument> ed_doc (detail (ed));


(2):

DOMElement* ed_el (ed_doc->getDocumentElement ()); // 'embedded_data' element
event_doc->adoptNode (ed_el); // ed_el not belongs to event_doc

// Now we need to insert the embedded_data element (ed_el) in the right
// place in the event document.
//

DOMElement* event_el (event_doc->getDocumentElement ()) // 'event' element

DOMNodeList* nl (event_el->getElementsByTagName("detail"));
DOMElement* detail_el (static_cast<DOMElement*> (nl->item (0)));

detail_el->appendChild (ed_el); // insert 'embedded_data' element


(3):

// See Xerces-C++ examples for more information.
//

DOMImplementation* impl (
  DOMImplementationRegistry::getDOMImplementation (
    [3]xml::string ("LS").c_str ()));

dom::auto_ptr<DOMWriter> writer (impl->createDOMWriter ());

if (writer->canSetFeature (XMLUni::fgDOMWRTDiscardDefaultContent, true))
  writer->setFeature (XMLUni::fgDOMWRTDiscardDefaultContent, true);

if (writer->canSetFeature (XMLUni::fgDOMWRTFormatPrettyPrint, true))
  writer->setFeature (XMLUni::fgDOMWRTFormatPrettyPrint, true);


LocalFileFormatTarget ft ("out.xml");

writer->writeNode (&ft, *event_doc);


hth,
-boris

References

   1. mailto:mwburn at mhpcc.hpc.mil
   2. xml::dom
   3. xml::string
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_xsd.zip
Type: application/octet-stream
Size: 18323 bytes
Desc: not available
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20060712/04a88983/test_xsd.obj


More information about the xsd-users mailing list