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 String getNamespaceURI(Document doc, String ns) {
    NamedNodeMap attr = doc.getDocumentElement().getAttributes();
    for (int i = 0; i < attr.getLength(); i++) {
        Node attrNode = attr.item(i);
        if (attrNode.getNodeName().indexOf(ns) != -1) {
            return (attrNode.getNodeValue());
        }//from w w w. j  ava2 s . c  o m
    }
    return null;
}

From source file:Main.java

static public String getNamespaceURI(Document doc, String prefix) {
    Element e = doc.getDocumentElement();
    return e.getAttributeNS("", prefix);
}

From source file:Main.java

private static Document getDocument(String filePath) throws Exception {
    File inputFile = new File(filePath);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(inputFile);
    doc.getDocumentElement().normalize();
    return doc;//from  w  ww .  j av a2  s  . c om
}

From source file:Main.java

public static Document readDocument(InputStream is) {
    try {/* ww w.jav  a 2  s .  c  o  m*/
        final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        final Document doc = docBuilder.parse(is);
        doc.getDocumentElement().normalize();
        return doc;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String toXMLString(String fileName) throws Exception {
    File file = new File(fileName);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    return toXMLString(doc);

}

From source file:Main.java

public static Document readDocument(String filePath)
        throws ParserConfigurationException, SAXException, IOException {
    File file = new File(filePath);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(file);
    document.getDocumentElement().normalize();
    return document;
}

From source file:Main.java

public static int getSize(Document doc, String tagName) {
    return getSize(doc.getDocumentElement(), tagName);
}

From source file:Main.java

public static String guessTokenFormat(Document document) {

    String minorVersion = document.getDocumentElement().getAttribute("MinorVersion");
    String majorVersion = document.getDocumentElement().getAttribute("MajorVersion");
    String version = document.getDocumentElement().getAttribute("Version");

    if ("1".equals(majorVersion) && "1".equals(minorVersion)) {

        return "SAML 1.1";
    } else if ("2.0".equals(version)) {

        return "SAML 2.0";
    } else {// w  w w .  j a va  2s.co m

        return null;
    }
}

From source file:Main.java

/**
 * Build a document from a InputStream./*from  w  ww  .  j  a  v  a2 s. c  o m*/
 *
 * @param is InputStream
 * @return a Document object
 * @throws Exception if an error occurs
 */
public static Document buildDocumentFromInputStream(InputStream is) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(is);
    doc.getDocumentElement().normalize();
    return doc;
}

From source file:Main.java

public static void addFragment(Document doc) {
    Element person;/*from   w  w  w .j a  va  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);
}