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 Document fromString(String text) {
    try {//  w  ww . ja  v a  2  s.c  o m

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(text);

        doc.getDocumentElement().normalize();

        return doc;

    } catch (IOException | ParserConfigurationException | SAXException e) {

    }

    return null;
}

From source file:Main.java

private static Element loadRootElement(InputStream inputStream) throws Exception {
    // Get XML document from the URL
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    //as a default, set validating to false
    factory.setValidating(false);/*w  ww. j av  a2s . c o m*/
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource inputSource = new InputSource(inputStream);
    Document doc = builder.parse(inputSource);
    return doc.getDocumentElement();
}

From source file:Main.java

public static void copyDocument(Document from, Document to) {
    Node node = to.importNode(from.getDocumentElement(), true);
    to.getDocumentElement().appendChild(node);
}

From source file:Main.java

public static Element getElementFromXml(String xmltext)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    StringReader sr = new StringReader(xmltext);
    InputSource is = new InputSource(sr);
    Document document = db.parse(is);
    return document.getDocumentElement();
}

From source file:Main.java

public static NodeList readXML(File xmlfile, String tag) throws Exception {
    NodeList list = null;//from w w w.  ja v a2 s.c om
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(xmlfile);
    Element rootElement = doc.getDocumentElement();
    list = rootElement.getElementsByTagName(tag);

    return list;
}

From source file:Main.java

private static Element getNewstudent() throws Exception {
    DocumentBuilder builder = dbFactory.newDocumentBuilder();
    Document newstudent = builder.parse(new StringBufferInputStream(studentData));
    Element student = newstudent.getDocumentElement();
    return student;
}

From source file:Main.java

public static synchronized Element createRootElement() {
    Element rootElement = null;//from   ww  w.  jav  a2s .  c  o  m
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = db.newDocument();
        rootElement = doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rootElement;
}

From source file:Main.java

public static NodeList getDomNodes(String file, String xmlTag) {
    NodeList nodes = null;/*  www .  ja  va 2  s .c  o  m*/

    try {
        File xml = new File(file);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xml);
        doc.getDocumentElement().normalize();

        nodes = doc.getElementsByTagName(xmlTag);
    } catch (Exception e) {
        e.printStackTrace();
        nodes = null;
    }

    logger.trace("Returning {} nodes", nodes.getLength());
    return nodes;
}

From source file:Main.java

/**
 * Creates W3C DOM Document object from XML file
 * @param filePath path to xml file including file name and extension
 *///  w w w . j a  va 2 s.com
public static Document parseXml(String filePath) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(filePath);
        doc.getDocumentElement().normalize();
        return doc;
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
        return null;
    } catch (SAXException se) {
        se.printStackTrace();
        return null;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Element getElement(Document doc, String tagName, int index) {
    NodeList rows = doc.getDocumentElement().getElementsByTagName(tagName);
    return (Element) rows.item(index);
}