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

/**
 * Reads the argument XML input stream.//  w w w.  j a v a 2  s.  c  o m
 * @param inStream An input stream carrying the only subject XML
 * @return The root node of the read DOM document.
 */
public static Node readXML(InputStream inStream) {
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        Document doc = builder.parse(inStream);
        inStream.close();
        return doc.getDocumentElement();
    } catch (SAXException saxEx) {
        throw new IllegalArgumentException(null, saxEx);
    } catch (IOException ioEx) {
        throw new IllegalArgumentException(null, ioEx);
    } catch (ParserConfigurationException parsEx) {
        throw new IllegalArgumentException(null, parsEx);
    } // try - catch
}

From source file:Main.java

public static Iterable<Node> eval(Document doc, String path) throws XPathExpressionException {
    return eval(doc.getDocumentElement(), path);
}

From source file:Main.java

public static void serializeXML(Document doc, Writer writer) throws IOException {
    serializeXML(doc.getDocumentElement(), writer, true);
}

From source file:Main.java

public static Document removeFromRootElement(Document document, String nodeName, String attributeName,
        String attributeValue) {// w  ww . j a v  a2  s .c o m
    try {
        Element rootElement = document.getDocumentElement();

        NodeList nl = document.getElementsByTagName(nodeName);
        Vector<Node> deletedNodes = new Vector<Node>();

        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            String noteAttributeValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();

            if (noteAttributeValue.equals(attributeValue)) {
                deletedNodes.add(node);
            }

        }

        for (Node deletedNode : deletedNodes) {
            rootElement.removeChild(deletedNode);
        }

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

    return document;
}

From source file:Main.java

/**
 * Reads the argument XML input String.//from w w w .  j  av a  2 s .  co m
 * @param xmlString The XML input String to be parsed.
 * @return The root node of the read DOM document.
 */
public static Node readXML(String xmlString) {
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
        return doc.getDocumentElement();
    } catch (SAXException saxEx) {
        throw new IllegalArgumentException("readXML() Caught SAXException: ", saxEx);
    } catch (IOException ioEx) {
        throw new IllegalArgumentException("readXML() Caught IOException: " + ioEx.getMessage(), ioEx);
    } catch (ParserConfigurationException parsEx) {
        throw new IllegalArgumentException("readXML() Caught ParserConfigurationException: ", parsEx);
    } // try - catch
}

From source file:DOMEdit.java

public static void addFragment(Document doc) {
        Element person;// w w w  .ja  v  a  2 s .  c o m
        Element root = doc.getDocumentElement();
        DocumentFragment fragment = doc.createDocumentFragment();
        person = makePersonNode(doc, "Name 1", "555-4444");
        fragment.appendChild(person);
        person = makePersonNode(doc, "Name 2", "555-9999");
        fragment.appendChild(person);
        root.appendChild(fragment);
    }

From source file:com.flipkart.phantom.runtime.impl.spring.utils.ConfigFileUtils.java

/**
 * Gets the task handler names from Config file
 * @param configFile job config file contents as a <code> ByteArrayResource </code>
 * @return List of task handler names, null if unable to find a TaskHandler name.
 *///from w w  w .  java  2s. c o  m
public static List<String> getHandlerNames(ByteArrayResource configFile) {
    List<String> jobNameList = new LinkedList<String>();
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(configFile.getInputStream());
        Element docEle = dom.getDocumentElement();
        //get a nodelist of nodes with the name "ConfigFileUtils.BATCH_JOB_TAG" 
        NodeList nl = docEle.getElementsByTagName(ConfigFileUtils.BATCH_JOB_TAG);
        //Loop over all found nodes
        if (nl != null && nl.getLength() > 0) {
            for (int i = 0; i < nl.getLength(); i++) {
                //get the element
                Element el = (Element) nl.item(i);
                if (el.hasAttribute(ConfigFileUtils.ID_PROP)) {
                    jobNameList.add(el.getAttribute(ConfigFileUtils.ID_PROP));
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("Unable to get the job name from the given Spring Batch configuration file", e);
        throw new PlatformException(e);
    }
    return jobNameList;
}

From source file:Main.java

/**
 * @param doc/*from  w  w w .  ja v a  2s  .  com*/
 * @param out
 * @throws IOException
 */
public static void serializeXML(Document doc, OutputStream out) throws IOException {
    serializeXML(doc.getDocumentElement(), out);
}

From source file:Main.java

public static Element loadDocument(File f, String defElement) throws Exception {
    Document doc = null;
    try {//from  ww w . j  a  v  a2  s  .  c om
        doc = getDocumentBuilder().parse(f);
        return doc.getDocumentElement();
    } catch (Exception se) {
        if (defElement != null)
            return getDocumentBuilder().newDocument().createElement(defElement);
        throw new Exception(se.getMessage());
    }
}

From source file:org.trpr.platform.batch.common.utils.ConfigFileUtils.java

/**
 * Gets the job names from Config file/*  w w  w .  ja va2 s  .c  o  m*/
 * @param configFile job config file contents as a <code> ByteArrayResource </code>
 * @return List of job names, null if unable to find a job name.
 */
public static List<String> getJobName(ByteArrayResource configFile) {
    List<String> jobNameList = new LinkedList<String>();
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(configFile.getInputStream());
        Element docEle = dom.getDocumentElement();
        //get a nodelist of nodes with the name "ConfigFileUtils.BATCH_JOB_TAG" 
        NodeList nl = docEle.getElementsByTagName(ConfigFileUtils.BATCH_JOB_TAG);
        //Loop over all found nodes
        if (nl != null && nl.getLength() > 0) {
            for (int i = 0; i < nl.getLength(); i++) {
                //get the element
                Element el = (Element) nl.item(i);
                if (el.hasAttribute(ConfigFileUtils.ID_PROP)) {
                    jobNameList.add(el.getAttribute(ConfigFileUtils.ID_PROP));
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("Unable to get the job name from the given Spring Batch configuration file", e);
        throw new PlatformException(e);
    }
    return jobNameList;
}