Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Carving out a subset of XML content into a separate XML file

2 views
Skip to first unread message

KP Bhat

unread,
Jan 9, 2008, 2:09:53 PM1/9/08
to
Reposting from the Xerces mailing list

David Bertoni wrote:
>
>
> 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

unread,
Jan 10, 2008, 3:44:23 PM1/10/08
to
On Jan 9, 1:09 pm, KP Bhat <kb...@sta.samsung.com> wrote:
> Reposting from the Xerces mailing list
>
> David Bertoni wrote:
>
> > 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;

0 new messages