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

时间:2016-02-09 22:05:57

标签: c++ xml xerces

我正在使用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 );
}

1 个答案:

答案 0 :(得分:1)

您应该使用WFXMLScanner扫描程序仅执行格式良好检查。 There are great examples关于如何使用xerces的特定扫描程序。

相关问题