Example usage for org.w3c.dom Document getDocumentElement

List of usage examples for org.w3c.dom Document getDocumentElement

Introduction

In this page you can find the example usage for org.w3c.dom Document getDocumentElement.

Prototype

public Element getDocumentElement();

Source Link

Document

This is a convenience attribute that allows direct access to the child node that is the document element of the document.

Usage

From source file:Main.java

public static Document parseXmlDocument(File file, boolean namespaceAware)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilder docBuilder = buildDocumentBuilder(namespaceAware);
    Document doc = docBuilder.parse(file);

    // normalize text representation
    doc.getDocumentElement().normalize();

    return doc;/*from  ww w . j ava  2 s . c o  m*/
}

From source file:Main.java

static Node getElementById(Document doc, String id, Map<String, Node> idMap) {
    if (!idMap.containsKey(id)) {
        registerIDs(doc, doc.getDocumentElement(), idMap);
    }//  ww  w. j ava  2 s.c o m
    return idMap.get(id);
}

From source file:Main.java

public static NodeList queryDomNodes(Document doc, String path) {
    return queryDomNodes(doc.getDocumentElement(), path);
}

From source file:Main.java

public static Element getElement(Document doc, String tagName, int index) {
    //Given an XML document and a tag return an Element at a given index
    NodeList rows = doc.getDocumentElement().getElementsByTagName(tagName);
    return (Element) rows.item(index);
}

From source file:Main.java

public static String queryDomString(Document doc, String path) {
    return queryDomString(doc.getDocumentElement(), path);
}

From source file:Main.java

/**
 * Traverses the DOM tree to lookup the XPath.
 * /*from  w w w  .j a  v a  2  s. c  om*/
 * A sample XPath would look like "Parent > Child > Grandchild" for a DOM
 * tree as following:
 * 
 * <pre>
 * <Parent>
 *       <Child>
 *          <Grandchild>
 *          </Grandchild>
 *       </Child>
 * </Parent>
 * </pre>
 * 
 * @param doc
 *            The DOM tree to traverse
 * @param xPath
 *            A path to lookup
 * @param separator
 *            String separator used to break up level hierarchy with in the
 *            path
 * @return Node(s) found at the given path
 */
public static NodeList lookup(Document doc, String xPath, String separator) {
    return lookup(doc.getDocumentElement(), xPath, separator);
}

From source file:DOMEdit.java

public static void addComment(Document doc) {
        Element root = doc.getDocumentElement();
        Element folks = (Element) root.getLastChild();
        Comment comment = doc.createComment("Text of the comment");
        root.insertBefore(comment, folks);
    }//from   w  ww  .ja v a  2  s. c  o  m

From source file:DOMEdit.java

public static void addProcessingInstruction(Document doc) {
        Element root = doc.getDocumentElement();
        Element folks = (Element) root.getLastChild();
        ProcessingInstruction pi;
        pi = (ProcessingInstruction) doc.createProcessingInstruction("validate", "phone=\"lookup\"");
        root.insertBefore(pi, folks);//w ww.j a  v a2s.c  om
    }

From source file:Main.java

/**
 * Prints the specified document.//from   w  ww  .j  ava  2s .  com
 * 
 * @param d the specified document.
 */
private static void printDocument(Document d) {
    printNode(d.getDocumentElement(), 0);
}

From source file:Main.java

/**
 * @param n//from w  w  w.  j av a 2s .  co m
 *            the node
 * @return the first element in the ancestor tree of {@code n}. If
 *         {@code n} is an {@link Element}, {@code n} is returned. If
 *         {@code n} is <code>null</code>, this method returns
 *         <code>null</code>.
 */
public static Element getAncestorElement(Node n) {
    if (n == null) {
        return null;
    }
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) n;
    }
    Node parent = n.getParentNode();
    if (parent == null) {
        Document doc = (Document) (n instanceof Document ? n : n.getOwnerDocument());
        return doc == null ? null : doc.getDocumentElement();
    }
    return getAncestorElement(parent);
}