使Xerces解析字符串而不是文件

And*_*dry 16 c++ xml parsing xerces-c

我知道如何使用XercesDOMParser从xml文件创建一个完整的dom:

xercesc::XercesDOMParser parser = new xercesc::XercesDOMParser();
parser->parse(path_to_my_file);
parser->getDocument(); // From here on I can access all nodes and do whatever i want
Run Code Online (Sandbox Code Playgroud)

嗯,这有用......但是如果我想解析一个字符串怎么办?就像是

std::string myxml = "<root>...</root>";
xercesc::XercesDOMParser parser = new xercesc::XercesDOMParser();
parser->parse(myxml);
parser->getDocument(); // From here on I can access all nodes and do whatever i want
Run Code Online (Sandbox Code Playgroud)

我正在使用版本3.在里面AbstractDOMParser我看到了解析方法及其重载版本,只解析文件.

如何从字符串中解析?

Fre*_*Foo 20

创建一个MemBufInputSourceparse那个:

xercesc::MemBufInputSource myxml_buf(myxml.c_str(), myxml.size(),
                                     "myxml (in memory)");
parser->parse(myxml_buf);
Run Code Online (Sandbox Code Playgroud)

  • 它是在错误消息中使用的"假系统ID","通过相对路径/ URL从该实体引用的任何实体将与此假系统ID相关".请参阅API文档. (4认同)
  • 好吧,我发现它......看到这里......荒谬的ahaha http://xerces.apache.org/xerces-c/faq-parse-2.html#faq-7 (2认同)

Dan*_*ger 12

使用以下XercesDOMParser :: parse()重载:

void XercesDOMParser::parse(const InputSource& source);
Run Code Online (Sandbox Code Playgroud)

传递一个MemBufInputSource:

MemBufInputSource src((const XMLByte*)myxml.c_str(), myxml.length(), "dummy", false);
parser->parse(src);
Run Code Online (Sandbox Code Playgroud)

  • 它位于`xercesc`命名空间,但你还需要`#include <xercesc/framework/MemBufInputSource.hpp>`.我迟到了两年,但我有同样的问题,其他人可以在以后再次使用它. (3认同)