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 loadDoc(String filePath) throws ParserConfigurationException, SAXException, IOException {
    File rawXml = new File(filePath);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(rawXml);
    doc.getDocumentElement().normalize();
    return doc;//w ww  .j a  v  a  2  s  .  com
}

From source file:Main.java

public static Document fileToXMLDoc(File file) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(file);

    doc.getDocumentElement().normalize();

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

From source file:Main.java

public static Element createRootElement(Document rootDoc, String rootTagName) {
    if (rootDoc.getDocumentElement() == null) {
        Element root = rootDoc.createElement(rootTagName);
        rootDoc.appendChild(root);//from  w  w  w .  j  av a 2 s.c  o m
        return root;
    }
    return rootDoc.getDocumentElement();
}

From source file:Main.java

/**
 * Returns Document object of the xml located at the given path
 * /*from  w  w w .  j a va  2s. c o  m*/
 * @param path path to xml
 * @return Document object
 * @throws ParserConfigurationException If parsing error occurs
 * @throws SAXException If parsing error occurs
 * @throws IOException If IO error occurs.
 */
public static Document getXMLDocument(String path)
        throws ParserConfigurationException, SAXException, IOException {
    File file = new File(path);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    return doc;
}

From source file:Main.java

static public Document inputStreamToDocument(InputStream inputStream) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(inputStream);
    doc.getDocumentElement().normalize();

    return doc;//from w  w  w .  j  av  a  2s  .com
}

From source file:Main.java

public static Node parseString(String string) throws Exception {
    InputStream is = new ByteArrayInputStream(string.getBytes("UTF-16"));

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    // from AIMLProcessor.evalTemplate and AIMLProcessor.validTemplate:
    //   dbFactory.setIgnoringComments(true); // fix this
    Document doc = dBuilder.parse(is);
    doc.getDocumentElement().normalize();
    Node root = doc.getDocumentElement();
    return root;//ww w  .  ja  v a  2s.com
}

From source file:Main.java

public static Node parseFile(String fileName) throws Exception {
    File file = new File(fileName);

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    // from AIMLProcessor.evalTemplate and AIMLProcessor.validTemplate:
    //   dbFactory.setIgnoringComments(true); // fix this
    Document doc = dBuilder.parse(file);
    doc.getDocumentElement().normalize();
    Node root = doc.getDocumentElement();
    return root;/*from  w w  w  .  java 2 s.  c  om*/
}

From source file:Main.java

public static void addNode(Document file, String text, String nodeName) {
    file.getDocumentElement().appendChild(file.createTextNode("  "));
    addOneElement(file, text, nodeName);
}

From source file:Main.java

/**
 * Traverses the passed Document <var>doc</var> and sets the "id" attribute
 * to be the user-defined ID attribute. This is used to find elements by
 * their ID. <br>//from   www .j  a  v  a 2 s .c o m
 * Specifically, this method simply gets the Document Element from
 * <var>doc</var> and passes it to defineIDAttribute().
 * 
 * @see XMLUtil#defineIDAttribute(Element)
 * @param doc
 */
public static void defineIDAttribute(Document doc) {
    Element e = doc.getDocumentElement();
    if (e != null)
        defineIDAttribute(e);
}

From source file:Main.java

public static Document getNormalizedDocumentFromFile(File file)
        throws SAXException, IOException, ParserConfigurationException {

    Document doc = getDocumentFromFile(file);
    doc.getDocumentElement().normalize();

    return doc;//from   ww w. ja va 2 s  .c  o m
}