/** * This parser takes 2 arguments: * i) the name of the file to parse * ii) whether or not to ignore whitespace. This value should be "true" * if you want whitespace to be ignored. * @author Nauman Chaudhry * @date Apr 6, 08 */ import javax.xml.parsers.*; import org.w3c.dom.*; import java.io.*; public class Ex5IgnoreWhitespace { public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); if(args.length == 2){ String setIgnore = args[1]; if(setIgnore != null && "true".equals(setIgnore)) { // We want the parser to element whitespace in element content. // Note that this will only eliminate white space which is directly // contained within element content that has an element only // content model. // Since this relies on the content model this setting requires that // the parser be in validation mode. dbf.setIgnoringElementContentWhitespace(true); dbf.setValidating(true); } } DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException pce) { pce.printStackTrace(); return; } Document myDoc = db.parse(args[0]); printNode(myDoc, ""); process(myDoc, "\t"); } // Process all the child nodes of the Node object passed in. Processing means: // i) print the node type, name, value // ii) call the process method with the node to process any children the node // has. private static void process(Node node, String indent) { Node c = null; NodeList nodes = node.getChildNodes(); if(nodes != null) for (int i = 0; i < nodes.getLength(); i++) { c = nodes.item(i); printNode(c, indent); if(c.getNodeType() == Node.ELEMENT_NODE) { // Get the attributes NamedNodeMap atts = c.getAttributes(); if(atts != null) { for(int k = 0; k < atts.getLength(); k++) { Node no = atts.item(k); printNode(no, indent); } } } process(c, indent + "\t"); } } private static void printNode(Node node, String indent){ System.out.println(indent + "node type = " + nodeTypeToString(node.getNodeType()) + ",\tname = " + node.getNodeName() + ",\tvalue = " + node.getNodeValue()); } // Return a string for the nodeType passed in. private static String nodeTypeToString(short nodeType){ String nodeString = "unknown"; switch (nodeType) { case Node.DOCUMENT_NODE: nodeString = "Document"; break; case Node.ELEMENT_NODE: nodeString = "Element"; break; case Node.ATTRIBUTE_NODE: nodeString = "Attribute"; break; case Node.TEXT_NODE: nodeString = "Text"; break; case Node.CDATA_SECTION_NODE: nodeString = "CDATA"; break; case Node.COMMENT_NODE: nodeString = "Comment"; break; case Node.PROCESSING_INSTRUCTION_NODE: nodeString = "Processing Instruction"; break; case Node.ENTITY_REFERENCE_NODE: nodeString = "Entity Reference"; break; case Node.DOCUMENT_TYPE_NODE: nodeString = "Document Type"; break; } return nodeString; } }