9 October 2010

Load and Save XML with DOM (Level 3)

Prior to DOM Level 3, DOM did not standardize reading and writing XML.
Below is the  SL-385 code 4-2 modified to use the standard DOM level 3 Load and Save (LS) API.
An alternative JAXP (but not DOM standard) way is to read using the JAXP DocumentBuilder and to write using the JAXP transformer (TrAX XSLT).
(I included a comment showing DOM LS writing starting from a DocumentBuilder obrained from JAXP reading as well.)


// SimpleDOML3LS.java
import org.w3c.dom.*;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.*;
import java.io.OutputStreamWriter;

public class SimpleDOML3LS {

  public static void main(String args[]) {
    Document doc;
    try {

      // Create DOM Document using DOM Level 3 Load 
      DOMImplementationLS ls = (DOMImplementationLS) DOMImplementationRegistry.
          newInstance().getDOMImplementation("LS");
      LSParser builder = ls.createLSParser(
        DOMImplementationLS.MODE_SYNCHRONOUS,
        null);
      doc = builder.parseURI(args[0]);

      // Obtain root elements
      Element root = doc.getDocumentElement();

      // Add comment texts
      Comment comment = doc.createComment("Training text");
      root.appendChild(comment);

      // Output to standard output; using DOM Level 3 save

      // If you parsed using a JAXP DocumentBuilder
      // you can also get your LSimplementation from your Document:
      // DOMImplementationLS ls = (DOMImplementationLS) doc.
      //   getImplementation().getFeature("LS","3.0");
      LSOutput target = ls.createLSOutput();
      target.setCharacterStream(new OutputStreamWriter(System.out));
      ls.createLSSerializer().write(doc, target);

    } catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
      System.exit(0);
    }
  }
}
To add newlines/indents to enhance readability of the output, replace in the above example
ls.createLSSerializer().write(doc, target);
with
      LSSerializer serializer = ls.createLSSerializer();
      DOMConfiguration serializerConfig = serializer.getDomConfig();
      serializerConfig.setParameter("format-pretty-print", Boolean.TRUE);
      serializer.write(doc, target);

No comments:

Post a Comment