0

我正在解析一个非常简单的 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.

有没有其他人遇到过这个?

4

2 回答 2

2

如果您使用的是 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;
}
于 2010-10-01T02:25:04.687 回答
0

看起来我的问题只是将解析声明更改为:

document.parse<parse_full>();

感谢 Karl 将我指向http://www.ffuts.org/blog/quick-notes-on-how-to-use-rapidxml/,这是一个很好的资源。

于 2010-10-01T13:27:13.733 回答