我想用C ++解析XML文件。我知道有很多库可用,例如libxml2或boost属性树。这种情况下的问题是XML包含processing instructions,例如
<root>
<?Instruction
<Info>Content</Info>
<Property>Value</Property>
?>
</root>
这些说明本身包含XML标记。
这是XML标准涵盖的内容吗? 我可以使用libxml2或任何其他解析器检索标签吗? 或者我可以将标记作为单个字符串,然后单独解析该字符串吗?
答案 0 :(得分:2)
虽然类似标记的处理指令数据看起来很不寻常,但它仍然是有效的XML,libxml2
会解析它。您可以使用xmllint
命令行工具和--sax
选项来了解SAX标记事件libxml2
将从中解析/生成的内容:
$ xmllint --sax test.xml
SAX.setDocumentLocator()
SAX.startDocument()
SAX.startElementNs(root, NULL, NULL, 0, 0, 0)
SAX.characters(
, 3)
SAX.processingInstruction(Instruction, <Info>Content</Info>
<Property>Value</Property>
)
SAX.characters(
, 1)
SAX.endElementNs(root, NULL, NULL)
SAX.endDocument()
虽然我没有经过测试,但毫无疑问libxml2
也会通过DOM API或xmlReader
API公开您的处理说明。