【发布时间】:2012-02-27 22:18:05
【问题描述】:
我正在尝试在我的 C++ 程序中读取 XML 文件。 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++ 了,所以我不能说出一个名字,但是用谷歌搜索一个 c++ 的 xml 解析器,我相信你会找到一些东西的。