【问题标题】:RapidXML XML parse errorRapidXML XML 解析错误
【发布时间】:2010-10-01 00:59:27
【问题描述】:

我正在解析一个非常简单的 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(),如下所示:

    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++ xml rapidxml


    【解决方案1】:

    如果你使用的是 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;
    }
    

    【讨论】:

      【解决方案2】:

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

      document.parse<parse_full>();
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-21
        • 1970-01-01
        • 1970-01-01
        • 2013-02-21
        • 2013-09-17
        • 1970-01-01
        • 1970-01-01
        • 2011-09-11
        相关资源
        最近更新 更多