FileDocCategorySizeDatePackage
DOMParserDemo.javaAPI DocExample6531Fri Jun 02 15:56:46 BST 2000None

DOMParserDemo

public class DOMParserDemo extends Object
DOMParserDemo will take an XML file and display the document using DOM.
author
Brett McLaughlin
version
1.0

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

This provides a command line entry point for this demo.

        if (args.length != 1) {
            System.out.println("Usage: java DOMParserDemo [XML URI]");
            System.exit(0);
        }

        String uri = args[0];

        DOMParserDemo parserDemo = new DOMParserDemo();
        parserDemo.performDemo(uri);
    
public voidperformDemo(java.lang.String uri)

This parses the file, and then prints the document out using DOM.

param
uri String URI of file to parse.

        System.out.println("Parsing XML File: " + uri + "\n\n");

        // Instantiate your vendor's DOM parser implementation
        DOMParser parser = new DOMParser();
        try {
            parser.setFeature("http://xml.org/sax/features/validation", true);
            parser.setFeature("http://xml.org/sax/features/namespaces", false);
            parser.parse(uri);
            Document doc = parser.getDocument();

            // Print the document from the DOM tree and
            //   feed it an initial indentation of nothing
            printNode(doc, "");

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error in parsing: " + e.getMessage());
        }

    
public voidprintNode(org.w3c.dom.Node node, java.lang.String indent)

This will print a DOM Node and then recurse on its children.

param
node Node object to print.
param
indent String spacing to insert before this Node

        switch (node.getNodeType()) {
            case Node.DOCUMENT_NODE:
                System.out.println("<xml version=\"1.0\">\n");
                // recurse on each child
                NodeList nodes = node.getChildNodes();
                if (nodes != null) {
                    for (int i=0; i<nodes.getLength(); i++) {
                        printNode(nodes.item(i), "");
                    }
                }
                break;

            case Node.ELEMENT_NODE:
                String name = node.getNodeName();
                System.out.print(indent + "<" + name);
                NamedNodeMap attributes = node.getAttributes();
                for (int i=0; i<attributes.getLength(); i++) {
                    Node current = attributes.item(i);
                    System.out.print(" " + current.getNodeName() +
                                     "=\"" + current.getNodeValue() +
                                     "\"");
                }
                System.out.println(">");

                // recurse on each child
                NodeList children = node.getChildNodes();
                if (children != null) {
                    for (int i=0; i<children.getLength(); i++) {
                        printNode(children.item(i), indent + "  ");
                    }
                }

                System.out.println(indent + "</" + name + ">");
                break;

            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                System.out.print(node.getNodeValue());
                break;

            case Node.PROCESSING_INSTRUCTION_NODE:
                System.out.println("<?" + node.getNodeName() +
                                   " " + node.getNodeValue() +
                                   "?>");
                break;

            case Node.ENTITY_REFERENCE_NODE:
                System.out.print("&" + node.getNodeName() + ";");
                break;

            case Node.DOCUMENT_TYPE_NODE:
                DocumentType docType = (DocumentType)node;
                System.out.print("<!DOCTYPE " + docType.getName());
                if (docType.getPublicId() != null)  {
                    System.out.print(" PUBLIC \"" + docType.getPublicId() + "\" ");
                } else {
                    System.out.print(" SYSTEM ");
                }
                System.out.println("\"" + docType.getSystemId() + "\">");
                break;
        }