Mike S
Mike S

Reputation: 11429

How to check if XML is well formed with Xerces

I'm using Xerces C++ lib to parse XML. I need a function that will determine if the inputted XML is well formed or not. There is some mention of "well formed" in the DOMConfiguration Interface but it does not explain how to use it.

This is how my code looks so far. It works the same regardless of whether the XML is well formed or not but I need to know when it is not well formed.

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 );
}

Upvotes: 2

Views: 656

Answers (1)

VladimirP
VladimirP

Reputation: 46

You should use WFXMLScanner scanner to perform well-formedness checking only. There are great examples on how to use specific scanners with xerces.

Upvotes: 1

Related Questions