2

I am trying to use RapidXML to parse xml content that looks like this:

<?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>

I have been able to access all of the direct children to open-psa using the following code

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

Now I am stuck. How do I access the elements nested in, say, define-gate name="top" As you might guess an actual .xml file may have a very large number of gates, basic events, parameters etc., and I do not think I can assume any particular ordering.

3 Answers3

1

node->next_sibling() gets you next node at the same level within the XML document. If you want to step into internal nodes of 'node', use first_node():

xml_node<>* nodeInternal = node->first_node();
Evgeny Sobolev
  • 438
  • 4
  • 12
0

Every node has the first_node() function, so inside of the if where you determine the node name, you can do another loop that starts with xml_node<>* child = node->first_node() and continues with child = child->next_sibling().

Tom Panning
  • 4,086
  • 2
  • 23
  • 45
0

Thanks for the tip. After some experimentation I came up with the following helper function to read a "gate". The conditional statements are nesting pretty deep, hence the helper. Works! again, thanks for the help!

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();
        }
    }
} 
}  
  • P.S, I am very new here. Reading this post it is not clear that my post is a result of help from Tom Panning. But it is , thanks 't – John Schroeder Dec 30 '12 at 03:28