如何使用rapidxml读取嵌套的xml

时间:2012-12-28 05:09:20

标签: c++ rapidxml

我正在尝试使用RapidXML来解析看起来像这样的xml内容:

<?xml version="1.0" ?>
<!DOCTYPE open-psa>
<open-psa>
  <define-gate name="top" >
    <or>
      <gate name="g1" />
      <gate name="g2" />
    </or>
  </define-gate>
  <define-basic-event name="e1">
    <exponential>
      <parameter name="lambda1" />
      <mission-time />
    </exponential>
  </define-basic-event>
  <define-parameter name="lambda1">
    <lognormal-deviate>
      <float value="2.0e-5" />
      <float value="3" />
      <float value="0.95" />
    </lognormal-deviate>
  </define-parameter>
</open-psa>

我已经能够使用以下代码访问所有直接子节点到open-psa

cout << "Importing fault tree...\n" ;
xml_document<> doc;
xml_node<> * root_node;
char* node_name;

// Read the xml file into a buffer
ifstream theFile ("SmallTree.xml");
vector<char> buffer((istreambuf_iterator<char>(theFile)),
                     istreambuf_iterator<char>());
buffer.push_back('\0');

// Parse the buffer
doc.parse<0>(&buffer[0]);

// Find the root node
root_node = doc.first_node("open-psa");

// Iterate over all child nodes
for (xml_node<> * node = root_node->first_node(); node; node = node->next_sibling())
{
    node_name = node->name();
    if (strcmp(node_name, "define-gate" ) == 0)
    {
        cout << node->name() << ", ";
        cout << node->first_attribute("name")->value() << endl;
    }
    else if (strcmp(node_name, "define-basic-event" ) == 0)
    {
        cout << node->name() << ", ";
        cout << node->first_attribute("name")->value() << endl;
    }
    else if (strcmp(node_name, "define-parameter" ) == 0)
    {
        cout << node->name() << ", ";
        cout << node->first_attribute("name")->value() << endl;
    }
}

现在我被卡住了。我如何访问嵌套的元素,比如,define-gate name =“top”你可能猜到一个实际的.xml文件可能有很多门,基本事件,参数等,我不认为我可以假设任何特定的顺序。

3 个答案:

答案 0 :(得分:0)

每个节点都有first_node()函数,因此在确定节点名称的if内部,您可以执行另一个以xml_node<>* child = node->first_node()开头并继续child = child->next_sibling()的循环。

答案 1 :(得分:0)

感谢您的提示。经过一些实验,我想出了以下的辅助函数来读取“门”。条件语句嵌套得非常深,因此是帮助器。作品!再次,谢谢你的帮助!

void readGate(xml_node<>* node)
{
// 
char* gname ;
xml_node<>* gtype = node->first_node();
if (gtype != 0)
{
    gname = gtype->name();
    if (strcmp(gname, "and" ) == 0)
    {
        // found an "and" gate, read children
        cout << "  " << gname << endl;
        xml_node<>* gin = gtype->first_node();
        while (gin != 0)
        {
            cout << "    " <<  gin->name() << ", ";
            cout << "    " <<  gin->first_attribute("name")->value();
            cout << endl;
            gin = gin->next_sibling();
        }
    }
    else if (strcmp(gname, "or" ) == 0)
    {
        // found an "or" gate, read children
        cout << "  " << gname << endl;
        xml_node<>* gin = gtype->first_node();
        while(gin != 0)
        {
            cout << "    " <<  gin->name() << ", ";
            cout << "    " << gin->first_attribute("name")->value();
            cout << endl;
            gin = gin->next_sibling();
        }
    }
} 
}  

答案 2 :(得分:0)

node-&gt; next_sibling()获取XML文档中同一级别的下一个节点。如果要进入'node'的内部节点,请使用first_node():

xml_node<>* nodeInternal = node->first_node();