我正在解析一个非常简单的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
当我使用默认设置解析它时,即 parse<0>(),如下所示:
ifstream file(xmlFile.c_str(), ifstream::in );
int length;
file.seekg (0, ios::end);
length = (int)file.tellg();
file.seekg (0, ios::beg);
char xmlBuf[ length + 1 ];
file.read( &xmlBuf[0], length );
xmlBuf[length] = '\0';
document.parse<0>(xmlBuf);
当使用 xml_node.value_size() 或 xml_node.name_size() 查询时,所有节点都在正确的位置并且具有正确的长度,但实际上将名称或值放入字符串甚至只是 printf(),它返回很多像下面这样的东西:
(........./.(..............-. <titlK.
还有其他人遇到过这个吗?
最佳答案
如果您使用的是 C++,为什么不用 C++ 的方式呢?
int main(int argc, char* argv[])
{
std::string buf;
std::string line;
std::ifstream in("file.xml");
// read file into buf
while(std::getline(in,line))
buf += line;
// After that, take a look on the traverse_xml() function implemented
// here: http://www.ffuts.org/blog/quick-notes-on-how-to-use-rapidxml/
// It has great stuff!
return 0;
}
关于c++ - RapidXML XML 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3835850/