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

/**
 * Creates the element mapping for the specified documents. For each node in
 * the source document an entry is created pointing to the corresponding
 * node in the destination object.//from   w w w  .  j a  v a 2 s. c  o  m
 *
 * @param doc1 the source document
 * @param doc2 the destination document
 * @return the element mapping
 */
private static Map<Node, Node> createElementMapping(Document doc1, Document doc2) {
    Map<Node, Node> mapping = new HashMap<Node, Node>();
    createElementMappingForNodes(doc1.getDocumentElement(), doc2.getDocumentElement(), mapping);
    return mapping;
}

From source file:Main.java

private final static String getElement(String filePath, String key) throws Exception {

    String value = null;/*from   w  w  w  .  java  2s  .com*/

    try {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

        File xmlFile = new File(filePath);

        Document document = documentBuilder.parse(xmlFile);

        Element resourcesElement = document.getDocumentElement();
        resourcesElement.normalize();

        NodeList strings = resourcesElement.getChildNodes();

        int stringsCount = strings.getLength();

        for (int i = 0; i < stringsCount; i++) {

            Node node = strings.item(i);

            short nodeType = node.getNodeType();

            if (nodeType == Node.ELEMENT_NODE) {

                Element element = (Element) node;

                String attribute = element.getAttribute("name");

                if (attribute.equals(key)) {

                    value = element.getTextContent();
                }
            }
        }

    } catch (Exception ex) {

        throw ex;
    }

    return value;
}

From source file:Main.java

/**
 * As a workaround for {@code javax.xml.transform.Transformer} not being able
 * to pretty print XML. This method prepares DOM {@code Document} for the transformer
 * to be pretty printed, i.e. providing proper indentations for enclosed tags.
 *
 * @param doc - DOM document to be pretty printed
 * @param ident - custom indentation as a string of white spaces
 *//*from w w w.  j  av  a  2  s  .c  om*/
public static void prettyFormat(Document doc, String ident) {
    doc.normalize();
    Element documentElement = doc.getDocumentElement();
    if (documentElement != null) {
        prettyFormat(documentElement, "", ident); //$NON-NLS-1$
    }
}

From source file:eu.delving.x3ml.X3MLCommandLine.java

static Element xml(InputStream inputStream) {
    try {//from   w  w  w .  j a va 2s  .c  om
        DocumentBuilder builder = documentBuilderFactory().newDocumentBuilder();
        Document document = builder.parse(inputStream);
        return document.getDocumentElement();
    } catch (Exception e) {
        throw exception("Unable to parse XML input");
    }
}

From source file:com.atomiton.watermanagement.ngo.util.WaterMgmtNGOUtility.java

public static String getXMLElementValue(String tagName, String xmlString) {
    try {/*  w w w. j ava 2s . com*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(xmlString)));
        Element rootElement = document.getDocumentElement();
        String rootElementTagName = rootElement.getTagName();
        if (rootElementTagName.equalsIgnoreCase(tagName)) {
            return rootElement.getTextContent();
        } else {
            NodeList list = rootElement.getElementsByTagName(tagName);
            if (list != null && list.getLength() > 0) {
                NodeList subList = list.item(0).getChildNodes();
                if (subList != null && subList.getLength() > 0) {
                    return subList.item(0).getNodeValue();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Gives the string representation of the XML element at the specified XPath
 * in the DOM tree/*  w  w w  . j a  v  a 2 s  . c o  m*/
 * 
 * @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 List<String> List of string representations of the Node(s) found
 *         at the given path
 */
public static List<String> text(Document doc, String xPath, String separator) {
    return text(doc.getDocumentElement(), xPath, separator);
}

From source file:Main.java

public static Document getDocumentForStream(InputStream stream) {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    Document doc = null;
    try {//  w ww . j  ava 2 s .c o  m
        docBuilder = docBuilderFactory.newDocumentBuilder();
        doc = docBuilder.parse(stream);
        doc.getDocumentElement().normalize();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return doc;
}

From source file:Main.java

/**
 * Returns the first element which name matchtes a given tag name.
 * //  w ww.  j  a  va  2s. c o m
 * @param doc the xml document in which to find an element of the given name
 * @param tagName the (tag) name of the desired child element
 * @return the child element if an element of that name existed, or null otherwise
 */
static public Element findFirstChildDeep(Document doc, String tagName) { // Child Element suchen
    if (doc == null)
        return null;
    return findFirstChildDeep(doc.getDocumentElement(), tagName);
}

From source file:Main.java

private static void getNodeList(Document doc, String nodeName, List<Node> result, Node node) {
    NodeList children;/*  w w w .jav a  2 s .  c  o m*/
    if (node == null) {
        Element root = doc.getDocumentElement();
        children = root.getChildNodes();
    } else {
        if (node.getNodeName().equals(nodeName)) {
            result.add(node);
            return;
        }
        children = node.getChildNodes();
    }
    if (children == null)
        return;
    for (int i = 0; i < children.getLength(); i++) {
        getNodeList(doc, nodeName, result, children.item(i));
    }
}

From source file:Main.java

public static Map<String, String> getNamespaceMap(Document document) {
    Map<String, String> nsMap = new HashMap<String, String>();

    NamedNodeMap map = document.getDocumentElement().getAttributes();
    for (int j = 0; j < map.getLength(); j++) {
        Node n = map.item(j);//from w w  w.j  a  v a 2s  .  c  o  m
        String attrName = ((Attr) n).getName();
        String attrValue = ((Attr) n).getValue();
        if (attrName != null && attrValue != null) {
            if (attrName.trim().startsWith("xmlns:")) {
                nsMap.put(attrValue, attrName.substring(6));
            }
        }
    }

    return nsMap;
}