如何检查 XML 是否与 Xerces 格式正确

How to check if XML is well formed with Xerces

本文关键字:Xerces 格式 是否 XML 何检查 检查      更新时间:2023-10-16

我正在使用Xerces C++ lib来解析XML。我需要一个函数来确定输入的 XML 格式是否正确。DOMConfiguration Interface中提到了"格式良好",但没有解释如何使用它。

这就是我的代码到目前为止的样子。无论 XML 格式是否正确,它的工作方式都相同,但我需要知道它何时格式不正确。

try {
    XMLPlatformUtils::Initialize();
    // create the DOM parser
    XercesDOMParser *parser = new XercesDOMParser;
    parser->setValidationScheme(XercesDOMParser::Val_Never);
    parser->parse(xml_input.c_str()); // where xml_input is my XML filename
    // get the DOM representation
    DOMDocument *doc = parser->getDocument();
    // get the root element
    DOMElement *root = doc->getDocumentElement();
}
catch ( DOMXPathException& e )
{
    char* message = xercesc::XMLString::transcode( e.getMessage() );
    cout << message << endl;
    XMLString::release( &message );
}

您应该使用 WFXMLScanner 扫描程序仅执行格式良好的检查。关于如何将特定扫描仪与xerces一起使用,有一些很好的例子。