我正在嘗試在我的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