NOTE: As of October 2009, this page has moved to http://wiki.fasterxml.com/StaxMateHowToDOM


How To make StaxMate work with DOM

As of version 2.0, it is possible to read DOM documents out of Stax stream readers (although not directly from SMInputCursor), and write DOM documents and fragments using Stax stream writers.

This is done by using converter class org.codehaus.staxmate.dom.DOMConverter.

Reading DOM document

DOM documents are read using XMLStreamReader:

FileInputStream in = new FileInputStream("input.xml");
XMLStreamReader sr = XMLInputFactory.newInstance().createXMLStreamReader(in);
// ... then do whatever processing (if any), and point to START_ELEMENT
// (or leave at START_DOCUMENT: that'll work too)
Document doc = new DOMConverter().buildDocument(sr);
in.close();

Writing DOM document

DOM documents are read using XMLStreamWriter:

FileOutputStream out = new FileOutputStream("output.xml");
XMLStreamWriter sw = XMLInputFactory.newInstance().createXMLStreamWriter(out);
// and output stuff, if need be...
new DOMConverter().writeDocument(doc, sw);
sw.close();
out.close();