2

これは私がこれまでに作成した最初のC++プログラムであり、ドキュメント内のxmlノードのリストを表示するはずです。TinyXMLを使用してまったく同じことを機能させましたが、Pugiの方がはるかに優れているので、引き続き使用したいと思います。

プログラムコード:

#include <iostream>
#include <string>
#include <vector>
using namespace std;


#include "pugixml/src/pugixml.hpp"
#include "pugixml/src/pugiconfig.hpp"
#include "pugixml/src/pugixml.cpp"
using namespace pugi;

const char * identify(xml_node node)
{
    const char * type;
    switch(node.type())
    {
        case node_null:
            type = "Null";
            break;
        case node_document:
            type = "Document";
            break;
        case node_element:
            type = "Element";
            break;
        case node_pcdata:
            type = "PCDATA";
            break;
        case node_cdata:
            type = "CDATA";
            break;
        case node_comment:
            type = "Comment";
            break;
        case node_pi:
            type = "Pi";
            break;
        case node_declaration:
            type = "Declaration";
            break;
        case node_doctype:
            type = "Doctype";
            break;
        default:
            type = "Invalid";
    }
    return type;
}

void walk(xml_node parent)
{
    printf("%s:\t%s\t%s\n", identify(parent), parent.name(), parent.value());
    for(xml_node child = parent.first_child(); child != 0; child = parent.next_sibling())
    {
        walk(child);
    }
}

int main(int argc, char* argv[])
{
    for (int i=1; i<argc; i++)
    {
        xml_document doc;
        xml_parse_result result = doc.load_file(argv[i]);

        cout << argv[i] << ": " << result.description() << endl;

        if (result)
        {
            walk(doc);
        }
    }

    return 0;
}

サンプルXML:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> 
<iOne>
    <iTwo>
        <iThree>
            <one>1</one>
            <two>2</two>
            <three>3</three>
        </iThree>
    </iTwo>

    <one>1</one>
    <two>2</two>
    <three>3</three>

</iOne>

コードは、2つの最初のコードに出くわして<three>無限ループに入るまで機能します。これにより、条件に問題があると思われますがfor(xml_node child = parent.first_child(); child != 0; child = parent.next_sibling())、すべてが例と同じですか?私はおそらくかなり明白な何かを逃しました...これらはc++での私の最初の赤ちゃんのステップです:)

私はC++のNULLがちょうど0であると理解するように与えられていますか?

また(複数の質問をしてすみません)、これは本当にpugiで何かをする正しい方法ですか?C ++プログラムの場合、ポインターをあまり使用していないようです。よくわかりません。

4

1 に答える 1

5

forそのループを次のように変更してみましたか?

for(xml_node child = parent.first_child(); child; child = child.next_sibling())

これは、サンプルがそれを行う方法です(たとえば、traverse_base.cpp )。

重要な部分はchild = child.next_sibling()、ではありませんparent.next_sibling()

于 2011-05-18T15:05:29.867 に答える