Discussion:
Carving out a subset of XML content into a separate XML file
(too old to reply)
KP Bhat
2008-01-09 19:09:53 UTC
Permalink
Reposting from the Xerces mailing list
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 now
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);
}
}
}
KP Bhat
2008-01-10 20:44:23 UTC
Permalink
Post by KP Bhat
Reposting from the Xerces mailing list
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 now
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).
I was able to get it working by tweaking the function API.


void
serializeSubtree_walker (DOMDocument* docPtr, DOMWriter&
theSerializer, XMLFormatTarget& target, const std::string&
topNodeName)
{
if(!docPtr) return;

DOMNode* startingNode = docPtr->getDocumentElement();
if(!startingNode) return;

DOMTreeWalker* walker = docPtr->createTreeWalker(startingNode,
DOMNodeFilter::SHOW_ALL, NULL, true);

DOMNode* nodePtr;
for(nodePtr = walker->nextNode(); nodePtr != NULL; nodePtr = walker-
Post by KP Bhat
nextNode())
{
if (nodePtr->getNodeType() == DOMNode::ELEMENT_NODE)
{
std::string thisNodeName = XMLString::transcode(nodePtr-
Post by KP Bhat
getNodeName());
if(thisNodeName == topNodeName)
theSerializer.writeNode(&target, *nodePtr);
}
}
}

Loading...