c++ - RapidXML XML 解析错误

标签 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>

当我使用默认设置解析它时,即 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/

相关文章:

Android studio 不会搜索 res/xml 文件夹来查找用法

c# - 使用 C# 将 xsd 转换为 xml

c++ - 传递具有可变维度的二维数组作为函数参数

c++ - STL 集中的指针委托(delegate)

android - 如何在 Android Studio xml 预览中启用布局装饰(例如 Actionbar)?

c++ - RapidXML 使用先前的属性值访问单个属性值?

c++ - 使用rapidxml读取和打印。打印多个 sibling C++ 的问题

c++ - 在控制台上显示 BMP 图像时堆损坏

c++ - 具有特定参数的特定构造函数的类型声明