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 Element getElementById(Document document, String id) {
    return getElementById(document.getDocumentElement(), id);
}

From source file:Main.java

public static void test() {
    String xml = "C:\\Users\\is96092\\Desktop\\music\\test.xml";
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {/* ww w.j a  va 2  s  . c o m*/
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(xml);
        Element doc = dom.getDocumentElement();
        String role1 = "";
        role1 = getTextValue(role1, doc, "note");
        System.out.println("result : " + role1);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Loads the xml file into an xml document and returns the root element.
 * /*from w w w .  j  a va 2 s  .c  o m*/
 * @param fileName
 *            the fully qualified name of the XML file to load; assumed not
 *            to be <code>null</code>.
 * 
 * @return root element of the xml document, never <code>null</code>.
 * 
 * @throws Exception
 *             on any error
 */
public static Element loadXmlResource(final String fileName) throws Exception {
    File file = new File(fileName);
    DocumentBuilder db = getDocumentBuilder();
    Document doc = db.parse(file);
    return doc.getDocumentElement();
}

From source file:Main.java

/**
 * internal function for adding an element to the DOM tree.
 *///from  w  w  w .java  2 s  .c om
private static void addNode(Document doc, String elemName, String value) {
    Element root = doc.getDocumentElement();
    Element elem = doc.createElement(elemName);
    elem.appendChild(doc.createTextNode(value));
    root.appendChild(elem);
}

From source file:Main.java

public static Node getRootNode(final String xmlContent) {
    final Document document = parse(xmlContent);
    return document.getDocumentElement();
}

From source file:Main.java

private static Element getRootNodeFromXmlStr(String xmlStr) {
    Element element = null;//w ww. j a va 2 s  .c  om
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream is = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));
        Document document = db.parse(is);
        element = document.getDocumentElement();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return element;
}

From source file:Main.java

private static void stripNamedElements(Document document, String name) {
    Element root = document.getDocumentElement();
    if (root != null) {
        NodeList nodes = root.getElementsByTagName(name);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            node.getParentNode().removeChild(node);
        }/*from w w w  .j  a va  2s. com*/
    }
}

From source file:Main.java

public static Node findNodeWithAttributeValue(Document d, String name, String attribute, String value) {
    Node body = d.getDocumentElement();

    return actualFindNodeWithAttributeValue(body, name, attribute, value);
}

From source file:Main.java

public static String getXPath(Element elt) {
    String xpath = "";
    if (elt != null) {
        Document doc = elt.getOwnerDocument();
        Element root = doc.getDocumentElement();
        Element parent = elt;//  ww  w .j  a va 2 s  . c  om
        while (parent != root) {
            xpath = "/" + parent.getNodeName() + xpath;
            parent = (Element) parent.getParentNode();
        }
    }
    return xpath;
}

From source file:Main.java

/**
 * returns a XML element.// w  w  w  .j  ava 2  s .co m
 *
 * @param pDocument         Document XML DOM document
 * @param psTagName         String XML node name
 * @param index             int XML node position
 * @return                  Element XML node element
 */
public static Element getElement(Document pDocument, String psTagName, int index) {
    NodeList rows = pDocument.getDocumentElement().getElementsByTagName(psTagName);
    return (Element) rows.item(index);
}