KP Bhat
2008-01-09 19:09:53 UTC
Reposting from the Xerces mailing list
created
a non-recursive version of my function, using the DOMWalker. It seems
to
iterate properly, but nothing is getting written to the file. I would
appreciate if someone could take a quick look and let me know what
silly
mistake I am making. For your convenience I am attaching both the
recursive
version of the function (which works) and the walker version of the
function
(which does not).
//////////////////////////////////////////
// Listing 1 (recursive --- works)
//////////////////////////////////////////
void
serializeSubtree_recursive (DOMNode* node, DOMWriter *theSerializer,
XMLFormatTarget *target, const std::string& topNodeName)
{
// May consider making topNodeName a global so that it
// does not get passed with every recursive call
//
if(!node) return;
std::string thisNodeName = XMLString::transcode(node-
theSerializer->writeNode(target, *node);
for (DOMNode* childNode (node->getFirstChild ()); childNode;
childNode = childNode->getNextSibling ())
{
serializeSubtree_recursive (childNode, theSerializer, target,
topNodeName);
}
}
//////////////////////////////////////////
// Listing 2 (using DOM Walker
// ----- does not work)
//////////////////////////////////////////
void
serializeSubtree_walker (DOMNode* startingNode, DOMWriter&
theSerializer,
XMLFormatTarget& target, const std::string& topNodeName)
{
if(!startingNode) return;
DOMDocument* docPtr = startingNode->getOwnerDocument();
if(!docPtr) return;
DOMTreeWalker* walker = docPtr->createTreeWalker(startingNode,
DOMNodeFilter::SHOW_ALL, NULL, true);
DOMNode* nodePtr;
for(nodePtr = walker->nextNode(); nodePtr != NULL; nodePtr =
walker->nextNode())
{
if (nodePtr->getNodeType() == DOMNode::ELEMENT_NODE)
{
std::string thisNodeName =
XMLString::transcode(nodePtr->getNodeName());
if(thisNodeName == topNodeName)
theSerializer.writeNode(&target, *nodePtr);
}
}
}
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
First of all, thanks to David Bertoni for his response. I have nowmarkup before and after you serialize the nodes? Writing out "<root>" and
</root> isn't terribly complicated.
Dave
created
a non-recursive version of my function, using the DOMWalker. It seems
to
iterate properly, but nothing is getting written to the file. I would
appreciate if someone could take a quick look and let me know what
silly
mistake I am making. For your convenience I am attaching both the
recursive
version of the function (which works) and the walker version of the
function
(which does not).
//////////////////////////////////////////
// Listing 1 (recursive --- works)
//////////////////////////////////////////
void
serializeSubtree_recursive (DOMNode* node, DOMWriter *theSerializer,
XMLFormatTarget *target, const std::string& topNodeName)
{
// May consider making topNodeName a global so that it
// does not get passed with every recursive call
//
if(!node) return;
std::string thisNodeName = XMLString::transcode(node-
getNodeName());
if(thisNodeName == topNodeName)theSerializer->writeNode(target, *node);
for (DOMNode* childNode (node->getFirstChild ()); childNode;
childNode = childNode->getNextSibling ())
{
serializeSubtree_recursive (childNode, theSerializer, target,
topNodeName);
}
}
//////////////////////////////////////////
// Listing 2 (using DOM Walker
// ----- does not work)
//////////////////////////////////////////
void
serializeSubtree_walker (DOMNode* startingNode, DOMWriter&
theSerializer,
XMLFormatTarget& target, const std::string& topNodeName)
{
if(!startingNode) return;
DOMDocument* docPtr = startingNode->getOwnerDocument();
if(!docPtr) return;
DOMTreeWalker* walker = docPtr->createTreeWalker(startingNode,
DOMNodeFilter::SHOW_ALL, NULL, true);
DOMNode* nodePtr;
for(nodePtr = walker->nextNode(); nodePtr != NULL; nodePtr =
walker->nextNode())
{
if (nodePtr->getNodeType() == DOMNode::ELEMENT_NODE)
{
std::string thisNodeName =
XMLString::transcode(nodePtr->getNodeName());
if(thisNodeName == topNodeName)
theSerializer.writeNode(&target, *nodePtr);
}
}
}