[xsd-users] Re: Adding namspaces
Boris Kolpackov
boris at codesynthesis.com
Wed Jul 12 12:43:20 EDT 2006
Hi Matt,
Matt Burnham <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 = 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 (
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20060712/a22e0002/attachment.pgp
More information about the xsd-users
mailing list