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);
    }
  }
}

Bhat
-- 
View this message in context: 
http://www.nabble.com/Carving-out-a-subset-of-XML-content-into-a-separate-XML-file-tp14627707p14705310.html
Sent from the Xerces - C - Users mailing list archive at Nabble.com.

Reply via email to