Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

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:

Code Block
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:

Code Block
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();