【问题标题】:RapidXML compilation error parsing stringRapidXML 编译错误解析字符串
【发布时间】:2012-07-21 05:36:20
【问题描述】:

我在使用 RapidXML 解析字符串时遇到了一些问题。我从 Eclipse 中收到一个错误,声称解析函数不存在。

make all 
Building file: ../search.cpp
Invoking: Cross G++ Compiler
g++ -DDEBUG -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"search.d" -MT"search.d" -o "search.o" "../search.cpp"
../search.cpp: In function ‘void search(CURL*, CURLcode, std::string, std::string)’:
../search.cpp:29:27: error: no matching function for call to ‘rapidxml::xml_document<>::parse(const char*)’
../search.cpp:29:27: note: candidate is:
../rapidxml-1.13/rapidxml.hpp:1381:14: note: template<int Flags> void rapidxml::xml_document::parse(Ch*) [with int Flags = Flags, Ch = char]
make: *** [search.o] Error 1

以下代码引发错误:

rapidxml::xml_document<> doc;    // This has no errors
doc.parse<0>(data.c_str());      // This line raises the error (data is a string)

这里是在线文档供参考: http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1parsing

RapidXML 有四个头文件:

  1. rapidxml_iterators.hpp
  2. rapidxml_print.hpp
  3. rapidxml_utils.hpp
  4. rapidxml.hpp

如何解决我的代码中的错误,我是否首先需要以某种方式解决标头中的编译器错误?

【问题讨论】:

    标签: c++ string xml-parsing rapidxml


    【解决方案1】:

    问题是在std::string 上调用c_str() 返回的char* 实际上是const char*,这对解析函数没有好处(解析实际上会更改它在 rapidXML 中解析的字符串)。这意味着我们需要在解析之前复制字符串

      xml_document<> doc;
      string str;                             // String you want to parse
      char* cstr = new char[str.size() + 1];  // Create char buffer to store string copy
      strcpy (cstr, str.c_str());             // Copy string into char buffer
    
      doc.parse<0>(cstr);                     // Pass the non-const char* to parse()
    
      // Do stuff with parsing
    
      delete [] cstr;                         // free buffer memory when all is finished
    

    我没有尝试编译上面的,所以可能有错误,关键是c_str() 返回一个const char*parse() 必须采用非常量char*。希望这可以帮助。至于你的标题,我通常只使用

     rapidxml.hpp
     rapidxml_print.hpp
    

    包含在我的源文件中。您没有链接器问题,因为 RapidXML 是仅标头实现(在我看来这很不错)。

    【讨论】:

    • 谢谢!这让我发疯了!
    • 或者,您可以通过在指向字符串第一个成员的指针上调用 parse 来避免复制。 doc.parse&lt;0&gt;(&amp;str[0]);
    • @Hydranix 我尝试了您的方法,但没有奏效。我仍在尝试找出一种方法来解析字符串,而不必通过将字符串复制到 char 数组来复制内存。如果我找到答案,我会在这里发布。
    • @Jaime '没有工作'是指它复制了字符串,还是根本没有解析?使用 gcc 6.3.1 我可以成功解析大量 std::strings of xml。
    【解决方案2】:

    正如@Hydranix 在 cmets 中指出的那样,

    doc.parse<0>(&str[0]); 
    

    效果很好。我使用它解析了一些大的 xml 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多