2

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 ファイルには非常に多数のゲート、基本イベント、パラメータなどが含まれている可能性があります。特定の順序を想定できます。

4

3 に答える 3

1

node->next_sibling() は、XML ドキュメント内の同じレベルにある次のノードを取得します。「ノード」の内部ノードにステップ インする場合は、first_node() を使用します。

xml_node<>* nodeInternal = node->first_node();
于 2014-02-26T06:29:44.007 に答える
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();
        }
    }
} 
}  
于 2012-12-30T03:22:09.463 に答える
0

すべてのノードには first_node() 関数があるため、ノード名を決定する場所内で、で始まり、で続くif別のループを実行できます。xml_node<>* child = node->first_node()child = child->next_sibling()

于 2012-12-28T22:37:01.640 に答える