Normalize All of the Text in a Document in Java

Description

The following code shows how to normalize All of the Text in a Document.

Example


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//from ww w  .j  ava 2 s . com
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

public class Main {
  static public void main(String[] arg) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc = null;
    try {
      DocumentBuilder builder = dbf.newDocumentBuilder();
      InputSource is = new InputSource("personWithDTD.xml");
      doc = builder.parse(is);

      normalize(doc);

    } catch (Exception e) {
      System.err.println(e);
    }
  }


  public static void normalize(Document doc) {
    Element root = doc.getDocumentElement();
    root.normalize();
  }
}




















Home »
  Java Tutorial »
    XML »




DOM
SAX