StaxMate How-To: Working 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:

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

Writing DOM document

DOM documents are read using XMLStreamWriter:

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


StaxMateHowToDOM (last edited 2011-02-22 18:46:31 by TatuSaloranta)

Copyright ©2009 FasterXML, LLC