c++ - 使用 PugiXML 进行 XML 解析,无限循环

标签 c++ xml mingw infinite-loop pugixml

这几乎是我制作的第一个 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>

代码一直有效,直到遇到两个 <three> 中的第一个为止。 s 并进入无限循环,这让我认为 for(xml_node child = parent.first_child(); child != 0; child = parent.next_sibling()) 中的条件有问题。但一切都与示例中相同?我可能错过了一些非常明显的东西...不过,这是我在 C++ 中迈出的第一步:)

我知道 C++ 中的 NULL 就是 0,对吧?

另外(抱歉问了多个问题),这真的是使用 pugi 做事的正确方法吗?对于C++程序,我似乎不太使用指针?我很困惑。

最佳答案

您是否尝试过将 for 循环更改为:

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

这就是示例的执行方式(例如 traverse_base.cpp)。

重要的部分是child = child.next_sibling(),而不是parent.next_sibling()

关于c++ - 使用 PugiXML 进行 XML 解析,无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6046728/

相关文章:

c++ 可以使用 fstream infile 从最近使用的 ofstream outfile 中收集数据吗?

xml - XSLT 应用模板选择中的 "@*|node()"是什么意思?

xml - 如何从 marshal 重新排序 xml 标签

c++ - 程序在cmd中运行,在cygwin中立即退出,状态为127,在emacs shell中没有输出

c++ - Hook 静态链接的 ELF 二进制文件

c++ - 将源代码添加到现有的 automake 程序

c++ - 如何将具有 16 位整数的图像转换为 QPixmap

android - 为动态创建的元素创建 XML

objective-c - 无法在 GNUstep/Win32 环境中构建 OCUnit - 找不到 objc/runtime.h

c++ - 在 Windows 的 Eclipse C++ 中运行 Hello World 应用程序