Pages

Root element name

As we have seen while doing the basic setup for Xerces, this framework is quite low level.

We have a confirmation of this impression when we try to write a function that just gets the name of a root element from an XML file.

Here is the code of such a function, that should be call after Xerces has been initialized, and before it is terminated:

#include <iostream>

#include <xercesc/dom/DOM.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/sax/SAXException.hpp>

XERCES_CPP_NAMESPACE_USE

void dumpRootName(const XMLCh* filename) // 1.
{
try
{
XercesDOMParser parser; // 2.
parser.parse(filename);
if(parser.getErrorCount() != 0)
std::wcerr << parser.getErrorCount() << " errors parsing " << filename << std::endl; // 3.
else
{ // 4.
DOMNode* doc = parser.getDocument();
DOMNode* root = doc->getFirstChild();
std::wcout << "Root name: " << root->getNodeName() << std::endl;
}
}
catch (const DOMException& e) // 5.
{
std::wcerr << "DOM Exception on " << filename << ": " << e.code << std::endl;
}
catch (const XMLException& e)
{
std::wcerr << "XML Exception on " << filename << ": " << e.getMessage() << std::endl;
}
catch (const SAXException& e)
{
std::wcerr << "SAX Exception on " << filename << ": " << e.getMessage() << std::endl;
}
catch (...)
{
std::wcerr << "Unexpected exception on " << filename << std::endl;
}
}

1. XMLCh is actually a synonim of wchar_t, so the function has as input parameter a wide char pointer to the filename in which is stored the XML we are interested in.
2. Actually we can't complain too much, Xerces provides us a lot of useful classes, among them XercesDOMParser is a highly configurable DOM parser that is what we need for our current task. Here we use it in its default configuration, that works just fine.
3. As we said, the filename is a wide char string, so we use a wide console stream to output it.
4. If no error has been detected, we extract the name from the root element. Since the parser succeeded, getDocument() should give back a valid XML document, seen as a DOM node that has one child node, the document root.
5. XercesDOMParser could throw DOMException, XMLException, SAXException, and we should always be prepared to catch something unexpected.

No comments:

Post a Comment