DomXmlSerializer.java - Serialize DOM to XML String

Q

How to serialize a DOM object to an XML string?

✍: FYIcenter

A

If you want to serialize (or convert) a DOM object to an XML string, you can follow these suggestions:

1. Build your DOM document object with the DOM API:

      DocumentBuilder b = f.newDocumentBuilder();
      Document d = b.newDocument();
      Element e = d.createElement("User"); 
      d.appendChild(e);
	  ...

2. Call the getDOMImplementation() method on the DocumentBuilder object to figure out DOM LS (Load and Save) impletation object. Then use it to create the LSSerializer object.

	  DOMImplementationLS ls = (DOMImplementationLS) b.getDOMImplementation();
      LSSerializer s = ls.createLSSerializer();
      System.out.println("DOMImplementationLS class:\n   "+ls.getClass().getName());
      System.out.println("LSSerializer class:\n   "+s.getClass().getName());

3. Call writeToString() method on the LSSerializer object to convert DOM object to an XML string:

	  System.out.println("XML from LSSerializer:\n"+s.writeToString(d));

4. Call getDomConfig() method on the LSSerializer object to retrieve the DOMCongiguratin object. Then set various parameters to control the LSSerializer

	  System.out.println("XML from LSSerializer:\n"+s.writeToString(d));

	  DOMConfiguration p = s.getDomConfig();
	  p.setParameter("format-pretty-print",Boolean.TRUE);
	  System.out.println("Pretty XML from LSSerializer:\n"+s.writeToString(d));

Here is a complete example of parsing an XML file with DOM API, DomXmlSerializer.java:

// Copyright (c) 2017 FYIcenter.com
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;

public class DomXmlSerializer {
   public static void main(String[] args) throws Exception {
      DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
      DocumentBuilder b = f.newDocumentBuilder();
      Document d = b.newDocument();

      Element e = d.createElement("User"); 
      d.appendChild(e);
	  
	  Element c = d.createElement("ID");
	  c.appendChild(d.createTextNode("202"));
	  e.appendChild(c);

	  c = d.createElement("Name");
	  c.appendChild(d.createTextNode("Frank Y. Ivy"));
	  e.appendChild(c);

	  c = d.createElement("BirthDate");
	  c.appendChild(d.createTextNode("1970-01-01"));
	  e.appendChild(c);
    
	  DOMImplementationLS ls = (DOMImplementationLS) b.getDOMImplementation();
      LSSerializer s = ls.createLSSerializer();
      System.out.println("DOMImplementationLS class:\n   "+ls.getClass().getName());
      System.out.println("LSSerializer class:\n   "+s.getClass().getName());
	  
	  System.out.println("XML from LSSerializer:\n"+s.writeToString(d));

	  DOMConfiguration p = s.getDomConfig();
	  p.setParameter("format-pretty-print",Boolean.TRUE);
	  System.out.println("Pretty XML from LSSerializer:\n"+s.writeToString(d));
   }
}

Compile and run the example program, DomXmlSerializer.java:

>\fyicenter\jdk-1.8.0\bin\javac DomXmlParserWhitespace.java

>\fyicenter\jdk-1.8.0\bin\java DomXmlParserWhitespace UserDTD.xml false

Implementation class:
   com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl
   
DOM object elements and text contents:
.User:
..#text:

 

Using XML DOM API with Apache Xerces

⇒⇒FAQ for Apache Xerces XML Parser

2017-12-13, 479👍, 0💬