From DOM to file by DOMLSSerializer

We have used XercesDOMParser to parse an XML in a DOM document, so that we could access and modify it programmatically.

If we want to work our way the other way round, getting a text representation of a DOM document, we can use DOMLSSerializer.

Actually, the code is not as simple as one could expect. I tried to keep it as short as I could, and here is the result:

void dumpDom(DOMNode* node) // 1.
DOMImplementationLS* impl = (DOMImplementationLS*)
DOMImplementationRegistry::getDOMImplementation(L"LS"); // 2.
if(impl == 0)

DOMLSSerializer* serializer = impl->createLSSerializer(); // 3.
if(serializer == 0)

StdOutFormatTarget ft; // 4.
DOMLSOutput* output = impl->createLSOutput(); // 5.
output->setByteStream(&ft); // 6.

try {
std::cout << "---" << std::endl;
serializer->write(node, output); // 7.
std::cout << std::endl << "---" << std::endl;
catch(const XMLException& xe) {
std::wcout << "XML Exception: " << xe.getMessage() << std::endl;
catch(const DOMException& de) {
std::wcout << "DOM Exception: " << de.getMessage() << std::endl;
catch (...) {
std::cout << "Unexpected Exception" << std::endl;

output->release(); // 8.

1. As input parameter the function expect a XML DOM node that would be dumped with all its children and grand-children (and so on). If we pass to this function the document node, as returned by the XercesDOMParser::getDocument(), for instance, the entire XML DOM document is printed.
2. We specify LS (Load and Save) as the feature the DOM implementation we retrieve should implement, and use the returned value as LS interface.
3. We ask to the implementation a serializer, the guy delegated to convert the DOM in a human readable form.
4. StdOutFormatTarget implements XMLFormatTarget for std::cout.
5. We should use a DOMLSOutput as output destination.
6. In this way we say that the output should go to std::cout
7. The real job is done in this line (!)
8. We release both output and serializer, to indicate that they are not in use anymore.

No comments:

Post a Comment