简体   繁体   English

在C ++中通过libxml2解析部分XML时过滤掉命名空间错误

[英]Filtering out namespace errors when parsing partial XML via libxml2 in C++

I have the need to parse partial XML fragments (which are presented as std::string), such as this one: 我需要解析部分XML片段(以std :: string表示),例如:

<FOO:node>val</FOO:node>

as xmlDoc objects in libxml2, and because these are fragments, I keep getting the namespace error : Namespace prefix FOO on node is not defined errors spit out into STDERR. 作为libxml2中的xmlDoc对象,并且因为这些是片段,我不断得到namespace error : Namespace prefix FOO on node is not defined错误吐出到STDERR中。

What I am looking for is for either a way to filter just these namespace warnings out or parse the XML fragment straight into a xmlNode object. 我正在寻找的是一种方法来过滤这些命名空间警告或直接将XML片段解析为xmlNode对象。

I think some sort of hacking around with initGenericErrorDefaultFunc() may be in order to go the first way, but the documentation for libxml2 is absolutely atrocious. 我认为使用initGenericErrorDefaultFunc()进行某种攻击可能是为了第一种方式,但libxml2的文档绝对是残酷的。

I would frankly prefer to go with the 2nd approach because it would require no error hacking and the node would be already aware of the namespace, but I don't think it's possible because the node has to have a root and XML fragments are not guaranteed to have only one root. 我坦率地倾向于采用第二种方法,因为它不需要错误黑客攻击,并且节点已经知道命名空间,但我不认为这是可能的,因为节点必须有根,并且不保证XML片段只有一个根。

I just need some guidance here of how to rid myself of the namespace error warning. 我只需要一些如何摆脱命名空间错误警告的指导。

Thank you very much. 非常感谢你。

Building on what @Potatoswatter said... can you create a context for the fragments? 建立在@Potatoswatter所说的......你能为片段创建一个上下文吗? Eg concatenate 例如连接

<dummyRoot xmlns:FOO="dummy-URI">

in front of your fragment, and 在你的片段前面,和

</dummyRoot>

afterward, then pass the concatenated string to xmlParseMemory(). 然后,将连接的字符串传递给xmlParseMemory()。

Alternatively, why don't you use xmlParseInNodeContext(), which lets you pass in a node to use as context (including namespaces), and the content can be any Well Balanced Chunk (eg multiple elements with no single root element). 或者,为什么不使用xmlParseInNodeContext(),它允许您传入一个节点以用作上下文(包括命名空间),并且内容可以是任何Well Balanced Chunk(例如,没有单个根元素的多个元素)。

Either method requires that you know, or can scan to find out, the set of all possible namespace prefixes that the fragment may use. 这两种方法都要求您知道或者可以扫描以找出片段可能使用的所有可能的名称空间前缀的集合。

是不是将xmlParserOptions XML_PARSE_NOERROR和/或XML_PARSE_NOWARNING传递给解析器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM