在C++;程序 我试图在我的C++程序中读取一个XML文件。XML文件如下所示: <?xml version="1.0" encoding="utf-8"?> <myprogram> <configuration> <window> <height> 300 </height> <width> 500 </width> </window> </configuration> </myprogram> ifstream in("mydata.xml"); //ignore the <?xml line in.ignore(200, '\n'); //i know that the first value i want is the window height so i can ignore <myprogram> <configuration> and <window> //ignore <myprogram> in.ignore(200, '\n'); //ignore <configuration> in.ignore(200, '\n'); //ignore <window> in.ignore(200, '\n'); string s; int height; //okay, now i have my height in >> s >> height; 300 500

在C++;程序 我试图在我的C++程序中读取一个XML文件。XML文件如下所示: <?xml version="1.0" encoding="utf-8"?> <myprogram> <configuration> <window> <height> 300 </height> <width> 500 </width> </window> </configuration> </myprogram> ifstream in("mydata.xml"); //ignore the <?xml line in.ignore(200, '\n'); //i know that the first value i want is the window height so i can ignore <myprogram> <configuration> and <window> //ignore <myprogram> in.ignore(200, '\n'); //ignore <configuration> in.ignore(200, '\n'); //ignore <window> in.ignore(200, '\n'); string s; int height; //okay, now i have my height in >> s >> height; 300 500,c++,xml,xerces,tinyxml,rapidxml,C++,Xml,Xerces,Tinyxml,Rapidxml,现在,我可以查看XML文件并尝试如下方式读取它: <?xml version="1.0" encoding="utf-8"?> <myprogram> <configuration> <window> <height> 300 </height> <width> 500 </width> </window> </configuration> </myprogram>

现在,我可以查看XML文件并尝试如下方式读取它:

<?xml version="1.0" encoding="utf-8"?>
<myprogram>
<configuration>
<window>
<height> 300 </height>
<width> 500 </width>
</window>
</configuration>
</myprogram>
ifstream in("mydata.xml");

//ignore the <?xml line
in.ignore(200, '\n');

//i know that the first value i want is the window height so i can ignore <myprogram> <configuration> and <window>

//ignore <myprogram>
in.ignore(200, '\n');

//ignore <configuration>
in.ignore(200, '\n');

//ignore <window>
in.ignore(200, '\n');

string s; int height;

//okay, now i have my height
in >> s >> height;
ifstreamin(“mydata.xml”);
//忽略>s>>高度;
一般来说,这似乎是一个坏主意,而且它确实限制了XML文件的修改方式。上面的解决方案非常手动,如果XML中有任何更改,那么整个读取方法似乎都必须更改


有更好的方法吗?

您可以使用具有解析XML功能的POCO库

您可以使用一些库来完成这项工作。如果您在Windows平台上工作,您可以使用Windows平台,它已经是系统的一部分

检查此问题:


其他流行的库:

boost属性树非常适合xml,我会使用它。

在多平台源代码中,我通常使用Qt xml阅读器

你有3种阅读方法:

  • -Qt读取XML的方式
  • -带内容处理类的标准SAX2读卡器
  • -具有XML节点的DOM文档读取器
  • 如果您只编写Windows软件,则应使用MSXML 6。因为WindowsXPSP3是操作系统的一部分


    在Linux上,您应该使用。

    您将需要一个XML解析器。外面有一群人:

    • 还附带了一个XML解析器(以及更多)
    • 仅当您已经使用Qt时

    我个人最喜欢的是PuGXML,但这是个人喜好的问题。

    我多年来一直没有使用C++,所以我不能说出一个谷歌,而是一个C++的XML解析器,我相信你会发现一些东西。MSXML听起来好像是我所要找的。这是一个Windows程序,所以我将首先尝试。谢谢你的提示。投票给RAPIDXML,因为它是一个头无依赖的库。为什么他可以在C语言库中有一个C++时结帐?@ PMR:为什么不呢?在许多情况下,纯C库速度更快。LIXXML2是常用的,通常是在机器上,所以他不会再依赖一个。在我的经验中,C库经常在习惯性C++中感到奇怪,并且需要一定数量的封装,并且不能与其他代码和更大的代码基集成得太好。但是,这可能是非常主观的。@ PMR:Linux上基于LIbxML2的许多C++ XML阅读器(例如:‘解析器’)。如果你检查一下,你会发现这个C库不是在编写地狱代码。但是基本上同意,所以我通常使用Qt的解析方式。我认为Boost用于属性树的RapidXml库并没有给您带来太多好处。除非您已经在使用Boost,否则这将增加一个很大的依赖项,而不是一个单一的头文件(RapidXml)。事实上,这完全取决于人们正在使用的库以及他们是否需要跨平台兼容性