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

/**
 * Returns root tagname/*from  ww w  .  j av a2  s.c om*/
 *
 * @param xml  The xml document to be searched.
 * @return String result.
 */
public static String getRootTagName(String xml) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    return document.getDocumentElement().getTagName();
}

From source file:Main.java

/**
 * Copies a document's content to another document (i.e. the root node is
 * adopted by the new document and inserted there). The new document should
 * be empty.//w w  w  . j a va  2 s.co  m
 *
 * @param oldDocument
 * @param newDocument
 *
 * @return the new containing document
 */
public static Document copyDocument(Document oldDocument, Document newDocument) {

    Node root = oldDocument.getDocumentElement();

    Node adoptedRoot = newDocument.adoptNode(root);

    newDocument.appendChild(adoptedRoot);

    return newDocument;
}

From source file:Main.java

/**
 * Given an XML string, return the textContent of all of the tags that match tagName as a list.
 * @param xmlStr//w  w w .  jav a2  s .  c o m
 * @param tagName
 * @method getValuesForNode
 * @static
 */
public static List<String> getValuesForNode(String xmlStr, String tagName) {

    List<String> values = new ArrayList<String>();
    try {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        InputStream is = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));

        Document doc = docBuilder.parse(is);

        doc.getDocumentElement().normalize();

        NodeList nodes = doc.getElementsByTagName(tagName);
        int nodeCount = nodes.getLength();

        for (int i = 0; i < nodeCount; i++) {
            Node node = nodes.item(i);
            values.add(node.getTextContent());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return values;

}

From source file:Main.java

public static String getNodeAttributeByName(String xmlFilePath, String nodeName, String attributeName) {
    String returnVal = "No value!";
    DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
    try {/*from   w  ww.ja  va 2  s.  c  om*/

        DocumentBuilder domBuilder = domBuilderFactory.newDocumentBuilder();
        InputStream is = new FileInputStream(xmlFilePath);
        Document doc = domBuilder.parse(is);
        Element root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    String test = node.getNodeName();
                    if (test.equals(nodeName)) {
                        returnVal = node.getAttributes().getNamedItem(attributeName).getNodeValue();
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return returnVal;
}

From source file:Main.java

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

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

    return doc;/*www . ja  va 2 s .c  o  m*/
}

From source file:Main.java

public static Element newTemplate(String xml) throws Exception {
    Document doc = readXML(xml);
    return doc.getDocumentElement();
}

From source file:Main.java

/**
 * Create an root element with the specified name
 *
 * @param doc//from  w  w  w .  j  a v  a2 s. c o  m
 * @param rootTagName
 * @return
 */
public static Element createRootElement(Document doc, String rootTagName) {
    if (doc.getDocumentElement() == null) {
        Element root = doc.createElement(rootTagName);
        doc.appendChild(root);
        return root;
    }
    return doc.getDocumentElement();
}

From source file:Main.java

public static Document attributize(Document doc) throws ParserConfigurationException {
    Element root = doc.getDocumentElement();
    attributize(root);/*  ww w  .  j  a  v a  2  s . c om*/
    return doc;
}

From source file:Main.java

public static Element getFirstChild(Document paramDocument, String paramString) {
    Element localElement = paramDocument.getDocumentElement();
    return getFirstChild(localElement, paramString);
}

From source file:Main.java

public static Document readXMLFile(File file) {

    try {/*from w ww .j  a v a2 s .c  o  m*/
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(new FileInputStream(file));
        doc.getDocumentElement().normalize();
        return doc;
    } catch (Exception e) {
        throw new RuntimeException("Error occured while reading xmlfile");
    }
}