让 Xerces 解析字符串而不是文件
我知道如何仅使用 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
嗯,这可行......但是如果我想解析字符串怎么办?就像
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
我正在使用版本 3 一样。查看 AbstractDOMParser
内部,我看到该解析方法及其重载版本,仅解析文件。
如何从字符串中解析?
I know how to create a complete dom from an xml file just using XercesDOMParser:
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
Well, that works... but what if I'd want to parse a string? Something like
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
I'm using version 3. Looking inside the AbstractDOMParser
I see that parse method and its overloaded versions, only parse files.
How can I parse from a string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建一个
MemBufInputSource
和解析
:Create a
MemBufInputSource
andparse
that:使用 XercesDOMParser::parse() 的以下重载:
向其传递 MemBufInputSource:
Use the following overload of XercesDOMParser::parse():
passing it a MemBufInputSource:
我用另一种方式做。如果这是不正确的,请告诉我原因。似乎有效。
这就是 parse 所期望的:
因此您需要放入 DOMLSInput 而不是 InputSource:
Im doing it another way. If this is incorrect, please tell me why. It seems to work.
This is what parse expects:
So you need to put in a DOMLSInput instead of a an InputSource:
您可以使用 xercesc/framework/MemBufInputSource.cpp 中的 MemBufInputSource,并且头文件 MemBufInputSource.hpp 包含大量文档,与上面的答案类似:
但请注意,除非您首先初始化系统,如下所示(取自 xerces-c-3.2.3/samples/src/SAX2Count/SAX2Count.cpp)
当然,读取文件不需要考虑这种类型的准备,因为您只需将文件路径传递给解析器采用的程序。因此,对于那些遇到段错误的人来说,这可能就是答案。
You may use MemBufInputSource as found in the xercesc/framework/MemBufInputSource.cpp, and the header file, MemBufInputSource.hpp contains extensive documentation, as similar to answers above:
But take note, this doesn't seem to work unless you first initialize the system, as below (taken from the xerces-c-3.2.3/samples/src/SAX2Count/SAX2Count.cpp)
Of course reading a file wouldn't require thinking about this type of prep since you just pass a file path to the program which the parser takes. So for those experiencing seg faults, this could be the answer.