RapidXML XML解析错误

时间:2010-10-01 00:59:28

标签: c++ xml rapidxml

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

当我使用默认设置解析它时,即解析&lt; 0&gt;(),如下所示:

    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.

还有其他人遇到过这个吗?

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

document.parse<parse_full>();

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