Ignorable Whitespace and Element Content : DOM Parser « XML « Java Tutorial






builderFactory.setNamespaceAware(true);        // Set namespace aware
   builderFactory.setValidating(true);            // and validating parser features
   builderFactory.setIgnoringElementContentWhitespace(true);
/*
Code revised from
Java, XML, and JAXP by Arthur Griffith John Wiley & Sons 2002

*/


import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class DOMCopy {
  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();
      builder.setErrorHandler(new MyErrorHandler());
      InputSource is = new InputSource("personWithDTD.xml");
      doc = builder.parse(is);

      write(doc);
    } catch (Exception e) {
      System.err.println(e);
    }
  }
  private static final String TAB = "    ";

  private static void write(Document doc) throws IOException {
    outputHeading(doc);
    outputElement(doc.getDocumentElement(), "");
  }

  private static void outputHeading(Document doc) {
    System.out.print("<?xml version=\"1.0\"");
    DocumentType doctype = doc.getDoctype();
    if (doctype != null) {
      if ((doctype.getPublicId() == null) && (doctype.getSystemId() == null)) {
        System.out.println(" standalone=\"yes\"?>");
      } else {
        System.out.println(" standalone=\"no\"?>");
      }
    } else {
      System.out.println("?>");
    }
  }

  private static void outputElement(Element node, String indent) {
    System.out.print(indent + "<" + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++) {
      Attr attr = (Attr) nm.item(i);
      System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
    }
    System.out.println(">");
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++)
      outputloop(list.item(i), indent + TAB);
    System.out.println(indent + "</" + node.getTagName() + ">");
  }

  private static void outputText(Text node, String indent) {
    System.out.println(indent + node.getData());
  }

  private static void outputCDATASection(CDATASection node, String indent) {
    System.out.println(indent +  node.getData());
  }

  private static void outputComment(Comment node, String indent) {
    System.out.println(indent + "<!-- " + node.getData() + " -->");
  }

  private static void outputProcessingInstructionNode(ProcessingInstruction node, String indent) {
    System.out.println(indent + "<?" + node.getTarget() + " " + node.getData() + "?>");
  }

  private static void outputloop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
      outputElement((Element) node, indent);
      break;
    case Node.TEXT_NODE:
      outputText((Text) node, indent);
      break;
    case Node.CDATA_SECTION_NODE:
      outputCDATASection((CDATASection) node, indent);
      break;
    case Node.COMMENT_NODE:
      outputComment((Comment) node, indent);
      break;
    case Node.PROCESSING_INSTRUCTION_NODE:
      outputProcessingInstructionNode((ProcessingInstruction) node, indent);
      break;
    default:
      System.out.println("Unknown node type: " + node.getNodeType());
      break;
    }
  }
}

class MyErrorHandler implements ErrorHandler {
  public void warning(SAXParseException e) throws SAXException {
    show("Warning", e);
    throw (e);
  }

  public void error(SAXParseException e) throws SAXException {
    show("Error", e);
    throw (e);
  }

  public void fatalError(SAXParseException e) throws SAXException {
    show("Fatal Error", e);
    throw (e);
  }

  private void show(String type, SAXParseException e) {
    System.out.println(type + ": " + e.getMessage());
    System.out.println("Line " + e.getLineNumber() + " Column " + e.getColumnNumber());
    System.out.println("System ID: " + e.getSystemId());
  }
}
//File: personWithDTD.xml
<?xml version="1.0" standalone="yes"?>

<!-- This document is both well formed and valid -->

<!DOCTYPE folks [
<!ELEMENT folks (person)*>
<!ELEMENT person (name, phone, email)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT email (#PCDATA)>
]>

<folks>
    <person>
        <name>B D</name>
        <phone>999 555-8888</phone>
        <email>b@xyz.net</email>
    </person>
</folks>


    
        
            B D
        
        
            999 555-8888
        
        
            b@xyz.net
        
    








33.2.DOM Parser
33.2.1.DOM Objects That Make Up the Parse Tree
33.2.2.A DOM Error Checker: Using DOM for Syntax Checking
33.2.3.A DOM Parse Tree Lister
33.2.4.Listing the Contents of Parse Tree Nodes: Using the DOM Parser to Extract XML Document Data
33.2.5.Ignorable Whitespace and Element Content
33.2.6.Remove the element from parent
33.2.7.Visiting All the Elements in a DOM Document
33.2.8.Getting the Root Element in a DOM Document
33.2.9.Getting a Node Relative to Another Node in a DOM Document
33.2.10.Getting the Notations in a DOM Document
33.2.11.Getting the Declared Entities in a DOM Document
33.2.12.Getting the Value of an Entity Reference in a DOM Document
33.2.13.Getting a DOM Element by Id
33.2.14.Converting an XML Fragment into a DOM Fragment
33.2.15.Parse an XML string: Using DOM and a StringReader.
33.2.16.Use DOM L3 DOMBuilder, DOMBuilderFilter DOMWriter and other DOM L3 functionality to preparse, revalidate and safe document.
33.2.17.Read XML as DOM
33.2.18.Create DOM Document out of string
33.2.19.Source To InputSource