DOM

JAXP

DOM


1. Overview

DOM (Document Object Model) creates an object-oriented model of an XML document. All elements are represented as objects organized into a hierarchical tree structure which corresponds to the hierarchy of the processed XML document. DOM is designed for documents (for example, articles and books) and from the JAXP 1.4.2 implementation supports XML Schema.

Advantages: DOM gives a possibility to navigate the tree structure, change elements and attributes, and create new XML documents in memory.

Disadvantages: DOM parsers are slow compared to SAX parsers, and consumes a lot of memory.

When to use: If the entire object model should be present in memory. DOM does not take advantage of Java's object-oriented features. So if there are used simple data structures without XML Schema, then more object-oriented standards, such as JDOM or dom4j can be a better choice.

2. javax.xml.parsers.DocumentBuilderFactory Example

// JAXP
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
// DOM
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class MyDOMParser {
    public static void main(String[] args) {
        try {
            DocumentBuilderFactory factory =
                    DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse("content.xml");
            printNodes(doc);

        } catch (ParserConfigurationException e) {
            System.out.println("Parser configuration exception.");
        } catch (FactoryConfigurationError e) {
            System.out.println("Factory configuration error.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printNodes(Node start) {
        String nodeName = start.getNodeName();
        System.out.println(nodeName + getNodeValue(start.getNodeValue()));

        if (start.getNodeType() == Node.ELEMENT_NODE) {
            printAttributes(start.getAttributes());
        }

        for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) {
            printNodes(child);
        }
    }

    private static void printAttributes(NamedNodeMap attributes) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attribute = attributes.item(i);
            System.out.println(" Attribute: " + attribute.getNodeName() + " = " + attribute.getNodeValue());
        }
    }

    private static String getNodeValue(String nodeValue) {
        if (nodeValue == null || nodeValue.trim().isEmpty()) {
            return "";
        }
        return " = " + nodeValue;
    }
}

Consider the following XML code snippet:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:ex="http://www.ec.com/EX/">
    <soap:Body>
        <ex:exam>
            <name xsi:type="xsd:string">DOM Parsers</name>
        </ex:exam>
    </soap:Body>
</soap:Envelope>

Our domparser example will generate the following analyzing this XML document:

#document
soap:Envelope
 Attribute: xmlns:ex = http://www.ec.com/EX/
 Attribute: xmlns:soap = http://schemas.xmlsoap.org/soap/envelope/
 Attribute: xmlns:xsd = http://www.w3.org/2001/XMLSchema
 Attribute: xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
#text
soap:Body
#text
ex:exam
#text
name
 Attribute: xsi:type = xsd:string
#text = DOM Parsers
#text
#text
#text

See also JAXP Overview, SAX, XSLT, StAX.



0 comments
Leave your comment: