简体   繁体   English

通过递归函数对XML树进行预排序遍历?

[英]Preorder traversal of an XML tree with a recursive function?

I have an XML file that looks like this: 我有一个看起来像这样的XML文件:

<?xml version="1.0"? encoding="UTF-8" standalone="no"?>
<dir name="mint">
  <dir name="pepper">
    <dir name="shauninman">
      <dir name="geomint">
        <file name="readme.txt"/ token="3">
        <file name="blank.gif"/ token="2">
        <file name="class.php"/ token="7"> 
      <dir/>
    <dir/>
  <dir/>
  <dir name="test1">
    <dir name="test2">
      <dir name="test3">
        <file name="foo1.txt"/ token="3">
        <file name="foo2.gif"/ token="5">
        <file name="foo3.php"/ token="5"> 
      <dir/>
      <dir name="test4">
        <file name="foo4.txt"/ token="3">
        <file name="foo5.gif"/ token="5">
        <file name="foo6.php"/ token="5"> 
      <dir/>
    <dir/>
  <dir/>
<dir/>

I once wrote a function in C++ that does a prefix traversal of a tree structure that looks like this: 我曾经用C ++写过一个函数,它对树结构进行前缀遍历,如下所示:

int traverseTree( Item* node, int id )
{
    cout << id;   
    id = id+1;
    vector<Item*>* children = node->getChildren();

    for( int i = 0; i < children->size(); ++i )
        id = traverseTree( children->at(i), id );

    return id;
}

Now, I want to do the same thing, but from the XML file. 现在,我想做同样的事情,但是要从XML文件开始。 In other words, I would like to parse the XML, then send the root to the function "traverseTree". 换句话说,我想解析XML,然后将根发送到函数“ traverseTree”。 Then, at each recursive call, I could retrive the children of the current node. 然后,在每个递归调用中,我都可以检索当前节点的子级。

How can I use a tool like xerces to accomplish this? 如何使用xerces之类的工具来完成此任务? What would be the new function? 新功能是什么?

Thanks for any help! 谢谢你的帮助!

#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>

#if defined(XERCES_NEW_IOSTREAMS)
#include <iostream>
#else
#include <iostream.h>
#endif

XERCES_CPP_NAMESPACE_USE

int main (int argc, char* args[]) {

    try {
        XMLPlatformUtils::Initialize();
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Error during initialization!"<< message << "\n";
        XMLString::release(&message);
        return 1;
    }

    XercesDOMParser* parser = new XercesDOMParser();
    parser->setValidationScheme(XercesDOMParser::Val_Always);
    parser->setDoNamespaces(true);    // optional

    ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
    parser->setErrorHandler(errHandler);

    char* xmlFile = "file.xml";

// PARSING ---- //解析----

    try {
        parser->parse(xmlFile);
    }
    catch (const DOMException& toCatch) {
        char* message = XMLString::transcode(toCatch.msg);
        cout << "Exception message is: \n"
             << message << "\n";
        XMLString::release(&message);
        return -1;
    }

// GETTING THE DOCUMENT //获取文档

    DOMDocument* inDoc  = parser->getDocument();
    /* from the document get the pointer to the root */
    DOMNode* inRoot = inDoc->getDocumentElement();

// TRAVERSING //遍历

int traverseTree( DOMNode* node, int id )
{
cout << id;   
id = id+1;
DOMNodeList* ch_list = node->getChildNodes();

for( int i = 0; i < ch_list->getLength(); ++i )
    id = traverseTree( ch_list->item(i), id );

return id;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM