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 Element getroot(String siteName) {
    Element root = null;//from  w  w w  .  j ava2 s. co  m
    try {
        File fXmlFile = new File(siteName);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document document = dBuilder.parse(fXmlFile);
        root = document.getDocumentElement();

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

    return root;

}

From source file:Main.java

public static Node findNode(Document d, String name) {

    Node body = d.getDocumentElement();

    return actualFindNode(body, name);
}

From source file:Main.java

/**
 * Append application specific tags into xml signature document
 * @param document//from   w w w.  j a  v  a 2 s . c  o  m
 * @return
 */
public static Document addUserInfoToSignature(Document document) {

    Element signature = document.getDocumentElement();

    // initially it has no root-element, ... so we create it:
    Element root = document.createElement("digital-signature");

    Element subjInfo = document.createElement("subject-information");
    subjInfo.appendChild(document.createElement("subject"));
    subjInfo.appendChild(document.createElement("date"));
    subjInfo.appendChild(document.createElement("time"));
    subjInfo.appendChild(document.createElement("timestamp"));

    root.appendChild(subjInfo);
    root.appendChild(signature);
    // we can add an element to a document only once,
    // the following calls will raise exceptions:
    document.appendChild(root);
    document.setXmlStandalone(true);

    return document;
}

From source file:Main.java

public static Element loadRootElement(DocumentBuilder documentBuilder, File file) throws Exception {
    Document document = loadDocument(documentBuilder, file);
    return document.getDocumentElement();
}

From source file:Main.java

/**
 * returns a XML node size.//from w  ww . j a v  a 2  s  .co m
 *
 * @param pDocument         Document XML DOM document
 * @param psTagName         String XML node name
 * @return                  int XML node size
 */
public static int getSize(Document pDocument, String psTagName) {
    NodeList rows = pDocument.getDocumentElement().getElementsByTagName(psTagName);
    return rows.getLength();
}

From source file:Main.java

/**
 * Converts a String representing an XML snippet into an {@link org.w3c.dom.Element}.
 *
 * @param xml snippet as a string//  w  ww .  j av  a  2  s.c o  m
 * @return a DOM Element
 * @throws Exception if unable to parse the String or if it doesn't contain valid XML.
 */
public static Element stringToElement(String xml) throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("utf8"));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document d = builder.parse(bais);
    bais.close();
    return d.getDocumentElement();
}

From source file:Main.java

static public Element getDocumentElement(InputStream in) {
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();

    try {//from  www.j a  v  a  2 s .co m
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(in);

        return doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * Parses an XML file and returns the name of the document element.
 * @param file the file containing the XML to parse.
 * @return the name of the document element, or null if the file does not parse.
 *//*w ww.  ja  va  2s. c  om*/
public static String getDocumentElementName(File file) {
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document d = db.parse(file);
        Element root = d.getDocumentElement();
        return root.getTagName();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static void insertLeadingPI(Document doc, String target, String data) {
    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    element.getParentNode().insertBefore(pi, element);
}

From source file:Main.java

public static void insertTrailingPI(Document doc, String target, String data) {
    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    element.getParentNode().appendChild(pi);
}