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:DOMEdit.java

public static void deleteFirstElement(Document doc) {
        Element root = doc.getDocumentElement();
        Element child = (Element) root.getFirstChild();
        root.removeChild(child);//w ww  .j a v a  2  s.c  om
    }

From source file:Main.java

private static Element convertXmlToElementWorker(String xml) throws Exception {
    Element element = null;/*from w w w .  j  a  va 2 s  . c om*/

    System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMImplementationSourceImpl");
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS,
            "http://www.w3.org/2001/XMLSchema");
    LSInput lsInput = impl.createLSInput();
    lsInput.setStringData(xml);
    Document doc = parser.parse(lsInput);
    if (doc != null) {
        element = doc.getDocumentElement();
    }
    return element;
}

From source file:DOMEdit.java

public static void modifyingTextbyCuttingandPasting(Document doc) {
        Element root = doc.getDocumentElement();
        Element place = (Element) root.getFirstChild();
        Text name = (Text) place.getFirstChild().getFirstChild();
        Text directions = (Text) place.getLastChild().getFirstChild();

        //    name.deleteData(offset,count);
        name.deleteData(2, 3);/*from www  .  j  a  va2 s .  c  o m*/

        //String bridge = directions.substringData(offset,count);
        String bridge = directions.substringData(2, 3);
        name.appendData(bridge);
    }

From source file:Main.java

public static Element findElement(Document doc, String elementNS, String elementName, String attrName,
        String attrValue) {//from  w ww. j  av a2 s .  co m
    return findElement(doc.getDocumentElement(), elementNS, elementName, attrName, attrValue);
}

From source file:Main.java

/**
 * Qualification of every element in a DOM document. Previous namespaces are
 * ignored.// w  w  w. j av  a 2s .  com
 * 
 * @param document
 *            The document to modify.
 * @param namespaces
 *            A map of prefix/URL where prefix are expected to match the
 *            local name of the elements they qualify (if an element has no
 *            corresponding prefix, it uses the namespace of its parent).
 */
public static void autoQualify(Document document, Map<String, String> namespaces) {
    Element root = document.getDocumentElement();
    for (Map.Entry<String, String> entry : namespaces.entrySet()) {
        String prefix = entry.getKey();
        String url = entry.getValue();
        StringBuilder attributName = new StringBuilder("xmlns");
        if (prefix != null) {
            attributName.append(':').append(prefix);
        }
        root.setAttribute(attributName.toString(), url);
    }
    autoQualify(document, root, namespaces, null);
}

From source file:Main.java

/**
 * Return the root element for specified document<br>
 * Create if it does not already exist with the specified name
 *///from  www .  j a v  a 2 s.c  om
private static Element getRootElement(Document doc, boolean create, String name) {
    if (doc != null) {
        Element result = doc.getDocumentElement();

        if ((result == null) && create) {
            result = doc.createElement(name);
            doc.appendChild(result);
        }

        return result;
    }

    return null;
}

From source file:Main.java

/**
 * @see //http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java
 *///from w w w.  j av a  2  s  .  c om
public static String toXML(Document document, boolean format) throws TransformerException {
    if (format) {
        removeWhitespaceNodes(document.getDocumentElement());
    }
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 2);
    Transformer transformer = transformerFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    Writer out = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(out));
    return out.toString();
}

From source file:DOMEdit.java

public static void editTextbyInsertionandReplacement(Document doc) {
        Element root = doc.getDocumentElement();
        Element place = (Element) root.getFirstChild();
        Text name = (Text) place.getFirstChild().getFirstChild();
        Text directions = (Text) place.getLastChild().getFirstChild();

        // Inserting the word "black"
        //    name.insertData(offset," black");
        name.insertData(3, " black");

        // Replace "left" with "right"
        //directions.replaceData(offset,count,"right");
        directions.replaceData(3, 3, "right");
    }//from   w  ww  . ja  va 2 s . c  om

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 Element getDocumentElement(File fileName) throws Exception {
    try {/*  w ww  .  j  a  v  a  2  s .  co m*/
        Document doc = getDocumentBuilder().parse(fileName);
        return doc.getDocumentElement();
    } catch (Exception se) {
        return null;
    }
}