Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用pugixml在代码块(C++)中打开XML文件_C++_Codeblocks_Pugixml - Fatal编程技术网

使用pugixml在代码块(C++)中打开XML文件

使用pugixml在代码块(C++)中打开XML文件,c++,codeblocks,pugixml,C++,Codeblocks,Pugixml,我一直在寻找如何使用代码块和库pugixml解析xml文件,但我尝试了不同的方法,但仍然不起作用 我必须解析的XML是一个图的房子,而我的C++程序是用Struts。 XML文件如下所示: <?xml version="1.0" encoding="UTF-8"?> <WATSON> <PHILIPS> 125 </PHILIPS> <PEREZ> 254 </PEREZ> <SANTOS> 222

我一直在寻找如何使用代码块和库pugixml解析xml文件,但我尝试了不同的方法,但仍然不起作用

我必须解析的XML是一个图的房子,而我的C++程序是用Struts。 XML文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<WATSON>
  <PHILIPS> 125 </PHILIPS>
  <PEREZ> 254 </PEREZ>
  <SANTOS> 222 </SANTOS>
</WATSON>
<PHILIPS>
    <CENTER> 121 </CENTER>
    <WATSON> 125 </WATSON>
    <SANTOS> 55 </SANTOS>
</PHILIPS>
<PEREZ>
    <WATSON> 254 </WATSON>
    <CENTER> 110 </CENTER>
</PEREZ>
等等

C++中的代码:重要部分:

int main(){
    pugi::xml_document file;

    if (!file.load_file("Sample.xml")){
    cout << "Error loading file XML." << endl;
    system("PAUSE");
    return -1;
}
    pugi::xml_node node;
    getNodeInfo(node);
    cin.get();
    return 0;
}

void getNodeInfo(xml_node node){
    for (xml_node_iterator it = node.begin(); it != node.end(); ++it)
    {
        cout << it->name() << "\n--------\n";
        system("PAUSE");
        for (xml_attribute_iterator ait = it->attributes_begin(); ait != it->attributes_end(); ++ait)
    {
            cout << " " << ait->name() << ": " << ait->value() << endl;
    }
        cout << endl;
        for (xml_node_iterator sit = node.begin(); sit != node.end(); ++sit)
        {
            getNodeInfo(*sit);
    }
}
}
请告诉我,代码中可能有什么错误?它总是进入if条件,我的意思是,它不会加载文件。
谢谢大家!

我注意到了几个错误

首先,您要向函数发送一个空节点,这样它就没有什么可处理的了。您应该改为发送加载的文件:

int main()
{
    pugi::xml_document file;

    xml_parse_result res;
    if(!(res = file.load_file("test.xml")))
    {
        cout << "Error loading file XML: " << res.description() << endl;
        system("PAUSE");
        return -1;
    }

    pugi::xml_node node; // you are sending an EMPTY node
    // getNodeInfo(node);

    // Send the file you just loaded instead
    getNodeInfo(file);

    cin.get();
    return 0;
}
另外,在函数中有一个奇怪的循环。您已经在节点的子节点上循环,不需要在相同的子节点上进行内部循环:

void getNodeInfo(xml_node node)
{
    for(xml_node_iterator it = node.begin(); it != node.end(); ++it)
    {
        cout << it->name() << "\n--------\n";
//      system("PAUSE");
        for(xml_attribute_iterator ait = it->attributes_begin();
        ait != it->attributes_end(); ++ait)
        {
            cout << " " << ait->name() << ": " << ait->value() << endl;
        }
        cout << endl;
// You are already in a loop for this node so no need for this
//      for(xml_node_iterator sit = node.begin(); sit != node.end(); ++sit)
//      {
//          getNodeInfo(*sit);
//      }

        // just use the iterator you are already looping over
        getNodeInfo(*it);
    }
}
最后,XML数据的格式不正确。它需要这样一个包罗万象的标签:


希望有帮助。

您的XML数据格式不正确。所有东西都应该有一个封装标签。这个问题和代码块有什么关系?谢谢你的回答。我希望我能知道XML文件,因为我在互联网上找到的文件是不同的,叫做graphml;但是这种xml文件格式是老师建议使用的。CODBROCK是我在C++上使用的空闲程序,我在其中安装了库PuGiXML。注意:PuGiXML没有强制执行单个文档元素规则,因为实际的问题太多文档打破了这一点。现在有了parse_片段,这可能会发生在2.0中,谁知道呢。非常感谢。这真的帮了大忙!关于xml文件,我有两个问题:什么是att=att1?函数getNodeInfo现在可以读取这个新文件的所有内容了吗@Iloveprogrammingwithcoffee我将att=att1添加到我可以测试打印属性的代码是否正常工作。是的,getNodeInfo是递归的,因此它将访问每个节点。实际上,它现在可以读取所有没有标签的内容,因为@zeuxcg说pugixml不是很挑剔。非常感谢您的帮助。你真的帮了我很多。现在它开始工作了。我只是将格式更改为graphml,它在变量方面工作得更好。Y
<?xml version="1.0" encoding="UTF-8"?>
<HOUSES>
    <WATSON att="att1">
        <PHILIPS> 125 </PHILIPS>
        <PEREZ> 254 </PEREZ>
        <SANTOS> 222 </SANTOS>
    </WATSON>
    <PHILIPS>
        <CENTER> 121 </CENTER>
        <WATSON> 125 </WATSON>
        <SANTOS> 55 </SANTOS>
    </PHILIPS>
    <PEREZ>
        <WATSON> 254 </WATSON>
        <CENTER> 110 </CENTER>
    </PEREZ>
</HOUSES>