Bhat wrote:
Hi,
I have written a small function that dumps portions of the DOM tree (rooted
at nodes with the specified tag name) to a file [see below]. Obviously the
output file does not have a root node. How can I make the output file a
proper XML file (with a single root node)? I have tried a few things, but
nothing seems to work. My function follows:
#define SPECIFIED_TAG “yadayada”
It would be better if you used the existing Xerces-C UTF-16 code point
statics to create a static string encoded in UTF-16, instead of the local
code page. Then you can skip transcoding. See src/xercesc/util/XMLUri.cpp
for some examples.
void
serializeSubtree (DOMNode* node, DOMWriter *theSerializer, XMLFormatTarget
*target)
{
if(!node) return;
const char* name = XMLString::transcode(node->getNodeName());
You should free this memory using XMLString::release(), or you will suffer
a memory leak. Also, this function can fail silently if the node name
contains characters not representable in the local code page.
See my previous comment for a better solution.
if(!strcmp(name, SPECIFIED_TAG))
{
theSerializer->writeNode(target, *node);
}
for (DOMNode* childNode (node->getFirstChild ()); childNode;
childNode = childNode->getNextSibling ())
{
serializeSubtree(childNode, theSerializer, target);
}
}
Why not try to low budget approach, and just write out a small amount of
markup before and after you serialize the nodes? Writing out "<root>" and
</root> isn't terribly complicated.
Dave