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 byteArrayToElement(byte[] cont) {
    if (cont == null || cont.length <= 0)
        return null;

    try {//from  w  ww  .  j  a va2s  .c om
        ByteArrayInputStream bais = new ByteArrayInputStream(cont);
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(false);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        InputSource is = new InputSource(bais);
        // is.setEncoding("gb2312");
        Document doc = docBuilder.parse(is);
        return doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Creates a {@link Node} out of a {@link String}
 * /*from   ww  w.j a  v a2  s  .  co  m*/
 * @param xmlString
 *            never <code>null</code>
 * @return the string as node, never <code>null</code>
 */
public static Node asNode(String xmlString) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    ByteArrayInputStream is = new ByteArrayInputStream(xmlString.getBytes());
    try {
        Document wfsCapabilities = builder.parse(is);
        return wfsCapabilities.getDocumentElement();
    } finally {
        is.close();
    }
}

From source file:Main.java

public static Document parseXml(InputStream inputStream)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(inputStream);
    doc.getDocumentElement().normalize();
    return doc;/*from  w  w w  . j av  a2 s. co  m*/
}

From source file:Main.java

public static Document getDocumentFromXML(String mappingFile, String schemaFile) {
    try {/* w ww .j a  v a  2  s . com*/
        if (schemaFile != null) {
            if (validateXML(schemaFile, mappingFile) == false) {
                return null;
            }
        }
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(mappingFile);
        doc.getDocumentElement().normalize();
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static synchronized Element getRootElement(InputStream is) {
    if (is == null) {
        return null;
    }/*  w  w  w.  j a  v  a  2  s.  c o  m*/
    Element rootElement = null;
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = db.parse(is);
        rootElement = doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rootElement;
}

From source file:Main.java

static public boolean addNamespaceURIs(Document doc, Map<String, String> nsMap) {
    Element root = doc.getDocumentElement();
    if (root == null)
        return false;
    // Add Namespace attributes
    for (Entry<String, String> entry : nsMap.entrySet()) {
        root.setAttribute("xmlns:" + entry.getKey(), entry.getValue());
    }/*from  w ww .ja v a 2  s  . c o  m*/
    return true;
}

From source file:Main.java

public static String retrieveDocumentAsString(Document doc) {
    Node node = doc.getDocumentElement();
    return retrieveNodeAsString(node);
}

From source file:Main.java

private static Element getRootNodeFromXmlUrl(String xmlUrl) {
    Element element = null;//from ww  w  . j ava  2  s .c om
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(xmlUrl);
        element = document.getDocumentElement();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return element;
}

From source file:Main.java

/**
 * Loads the imput stream and returns the root element.
 * /*from w w  w . ja v  a  2 s.  c o  m*/
 * @param in
 *            Input Stream.
 * 
 * @return root element of the xml document, never <code>null</code>.
 * 
 * @throws Exception
 *             on any error
 */
public static Element loadXmlResource(final InputStream in) throws Exception {
    DocumentBuilder db = getDocumentBuilder();
    Document doc = db.parse(in);
    return doc.getDocumentElement();
}

From source file:Main.java

public static Element unmarshalDOMElement(byte[] input)
        throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(new ByteArrayInputStream(input));

    return doc.getDocumentElement();
}