pugixml没有加载文件

时间:2014-11-09 00:33:14

标签: xml pugixml

我正在尝试加载“tree.xml”,它与loadfile.cpp在同一目录中,该文件也有pugixml.cpp,pugixml.hpp,pugiconfig.hpp。

#include "pugixml.cpp"
#include <iostream>
using namespace std;

int main()
{
    pugi::xml_document doc;

    pugi::xml_parse_result result = doc.load_file("tree.xml");

    if (doc.load_file("file.xml")){
        cout << "success" << endl;
    }else{
        cout << "fail" << endl;
    }

    cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << endl;
}

我得到了结果:

fail
Load result: No error, mesh name: 

我在这里关注手册: “加载Documnet”下的http://cdn.rawgit.com/zeux/pugixml/v1.4/docs/quickstart.html

tree.xml文档如下所示:

<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

为什么文件没有加载?我无法弄清楚什么是错的。

1 个答案:

答案 0 :(得分:1)

没关系。我找到了解决方案。

#include "pugixml.cpp"
#include <iostream>
using namespace std;

int main()
{
    pugi::xml_document doc;

    pugi::xml_parse_result result = doc.load_file("tree.xml");

    if (result)){  // CHANGE THIS LINE TO RESULT INSTEAD OF doc.load_file("tree.xml")
        cout << "success" << endl;
    }else{
        cout << "fail" << endl;
    }

    cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << endl;
}