Filtering namespace errors when parsing partial XML via libxml2 in C ++

I need to parse partial XML fragments (which are represented as std :: string), for example:

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

as xmlDocobjects in libxml2, and since these are fragments, I keep getting errors namespace error : Namespace prefix FOO on node is not definedthat spit out in STDERR.

What I'm looking for is a way to filter only these warnings from a namespace or parse an XML fragment directly into an object xmlNode.

I think some hacking with initGenericErrorDefaultFunc () may be for going the first way, but the documentation for libxml2 is absolutely terrible.

I would frankly prefer to go with the second approach, because this would not require hacking errors, and node would already know about the namespace, but I do not think that this is possible, because node must have root and XML fragments are not guaranteed to have only one root.

I just need some guidance here on how to get rid of the namespace error warning.

Many thanks.

+3
source share
2 answers

Based on what @Potatoswatter said ... can you create context for fragments? For instance. concatenation

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

in front of your fragment and

</dummyRoot>

then pass the concatenated string to xmlParseMemory ().

, xmlParseInNodeContext(), node ( ), (, ).

, , , , .

+3

xmlParserOptions XML_PARSE_NOERROR / XML_PARSE_NOWARNING ?

0

Source: https://habr.com/ru/post/1754329/


All Articles