I am trying to create a xmlNode
from an XML string with xmlParseBalancedChunkMemory
but when the XML string containing a prefix/namespace in it, libxml2
would throw error 201 (XML_NS_ERR_UNDEFINED_NAMESPACE
). What is the proper way to parse an XML string?
The code I used to parse the XML string:
xmlNodePtr newNode = nullptr;
// int rc = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0,
// BAD_CAST content,
// &newNode);
int rc = xmlParseBalancedChunkMemoryRecover(doc, NULL, NULL, 0,
BAD_CAST content,
&newNode, 1);
The XML string I’m trying to parse:
<ds:X509Certificate>MII...blah</ds:X509Certificate>
The error being thrown:
libxml<Err>: namespace
libxml<Err>: error :
libxml<Err>: Namespace prefix ds on X509Certificate is not defined
libxml<Err>: <ds:X509Certificate>MII...blah</ds:X509Certificate>
libxml<Err>:
I don’t understand why it throws that error when the doc
being passed already contains the namespace definitions.
<EDSCrate xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
PS: the same code worked fine on normal, non-prefixed, XML strings such as <a>helloworld</a>
or helloworld
(text node).
5