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:com.opensymphony.xwork2.config.providers.XmlHelper.java

/**
 * Return the value of the "order" attribute from the root element
 *///www.  j  a v a 2  s  . c o  m
public static Integer getLoadOrder(Document doc) {
    Element rootElement = doc.getDocumentElement();
    String number = rootElement.getAttribute("order");
    if (StringUtils.isNotBlank(number)) {
        try {
            return Integer.parseInt(number);
        } catch (NumberFormatException e) {
            return Integer.MAX_VALUE;
        }
    } else {
        //no order specified
        return Integer.MAX_VALUE;
    }
}

From source file:Main.java

public static Source getDomSourceIgnoringDtd(InputStream inputStream)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // stop the loading of DTD files
    factory.setValidating(false);/* w  ww . ja  v a  2 s.  c o  m*/
    factory.setNamespaceAware(true);
    factory.setFeature("http://xml.org/sax/features/validation", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    DocumentBuilder docbuilder = factory.newDocumentBuilder();

    Document doc = docbuilder.parse(inputStream);

    Source domSource = new DOMSource(doc.getDocumentElement());

    return domSource;
}

From source file:de.betterform.cache.CacheManager.java

/**
 *
 * @param file cache key//  w ww  . j av a 2 s.  c o m
 * @return document cache value 
 * @throws XFormsException
 */
public static Document getDocument(File file) throws XFormsException {
    Element elem = getElementFromFileCache(file);
    if (elem == null) {
        return null;
    }

    Document cachedDoc = (Document) elem.getObjectValue();
    org.w3c.dom.Element elment = cachedDoc.getDocumentElement();
    Document document = DOMResource.newDocument();
    //document.appendChild(document.adoptNode(elment.cloneNode(true)));
    document.appendChild(document.importNode(elment, true));
    return document;
}

From source file:Main.java

public static String documentToFragmentString(Document document) throws Exception {
    return nodeToString(document.getDocumentElement(), new HashSet(),
            document.getDocumentElement().getNamespaceURI());
}

From source file:eu.dasish.annotation.backend.Helpers.java

public static Element stringToElement(String string)
        throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilder dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputStream is = new ByteArrayInputStream(string.getBytes("UTF-16"));
    Document doc = dbf.parse(is);
    return doc.getDocumentElement();

}

From source file:DOMEdit.java

private static void write(Document doc) throws IOException {
    outputHeading(doc);//from  w ww  .  ja v  a  2 s .c o m
    outputElement(doc.getDocumentElement(), "");
}

From source file:com.cuubez.visualizer.processor.ConfigurationProcessor.java

private static String getRootNodeName(final Document document) {

    Node node = document.getDocumentElement();
    return node.getNodeName();

}

From source file:Main.java

public static Document parseXmlDocument(String xml, boolean namespaceAware)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilder docBuilder = buildDocumentBuilder(namespaceAware);
    Document doc = docBuilder.parse(new InputSource(new StringReader(xml)));

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

    return doc;//from   w  w  w .  j  a  v a 2 s. co m
}

From source file:Main.java

public static List<String> getNodeValues(String response, String tagName)
        throws ParserConfigurationException, SAXException {
    try {//from ww  w. jav  a2 s  .  c  om
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new ByteArrayInputStream(response.getBytes()));

        Element rootElement = document.getDocumentElement();

        List<String> arrayList = new ArrayList<>();
        NodeList list = rootElement.getElementsByTagName(tagName);
        NodeList subList;
        if (list != null && list.getLength() > 0) {
            for (int k = 0; k < list.getLength(); k++) {
                subList = list.item(k).getChildNodes();
                if (subList != null && subList.getLength() > 0) {
                    arrayList.add(subList.item(0).getNodeValue());
                }
            }
            return arrayList;
        }
    } catch (Exception e) {
        return null;
    }
    return null;
}

From source file:DOMEdit.java

public static void dupAttributes(Document doc) {
        Element root = doc.getDocumentElement();
        Element personOne = (Element) root.getFirstChild();

        Attr deptAttr = personOne.getAttributeNode("dept");
        personOne.removeAttributeNode(deptAttr);
        String deptString = deptAttr.getValue();
        personOne.setAttribute("dept", deptString + " updated");

        String mailString = personOne.getAttribute("mail");
        personOne.setAttribute("mail", mailString + " updated");

        String titleString = personOne.getAttribute("title");
        //personOne.removeAttribute("title");
        personOne.setAttribute("title", titleString + " updated");
    }/* w w  w. j  av a 2  s. co  m*/