我正在尝试在我的C++程序中读取XML文件。 XML文件看起来是这样的:在C++程序中读取XML文件
<?xml version="1.0" encoding="utf-8"?>
<myprogram>
<configuration>
<window>
<height> 300 </height>
<width> 500 </width>
</window>
</configuration>
</myprogram>
现在我可以看一下XML文件,并试图这样来阅读:
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;
一般来说,这似乎是一个坏主意,它真的限制了XML文件的修改方式。上述解决方案非常手动,如果XML中的任何内容发生更改,似乎读取它的整个方法都必须更改。
有没有更好的方法来做到这一点?
我没有带用C++多年,所以我不能命名一个但谷歌的XML解析器C++,我相信你会找到的东西。 – Dan675 2012-02-27 22:21:12