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

/**
 * Creates W3C DOM Document object from XML file
 * @param filePath path to xml file including file name and extension
 *///from  w  w  w .  ja  va 2s .  co m
public static Document parseXml(String filePath) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(filePath);
        doc.getDocumentElement().normalize();
        return doc;
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
        return null;
    } catch (SAXException se) {
        se.printStackTrace();
        return null;
    } catch (IOException ioe) {
        //ioe.printStackTrace();
        String toRemove = File.separator + "WebApp" + File.separator;
        Document doc;
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(filePath.replace(toRemove, File.separator));
            doc.getDocumentElement().normalize();
            return doc;
        } catch (SAXException saxex) {
            saxex.printStackTrace();
            return null;
        } catch (ParserConfigurationException pcex) {
            pcex.printStackTrace();
            return null;
        } catch (IOException ioex) {
            ioex.printStackTrace();
            return null;
        }
    }
}

From source file:Main.java

@Nullable
public static Element getDocumentElement(@Nullable final Node aNode) {
    final Document aDoc = getOwnerDocument(aNode);
    return aDoc == null ? null : aDoc.getDocumentElement();
}

From source file:Main.java

/**
 * Check that the name of root element of the given {@code document} matches the given {@code expectedRootElementName}
 *
 * @param document/* w w  w .ja  va 2  s. c om*/
 * @param expectedRootElementName
 */
public static void checkRootElement(@Nonnull Document document, @Nullable String expectedRootElementName) {
    if (document.getDocumentElement() == null || expectedRootElementName == null) {
        return;
    } else if (!expectedRootElementName.equals(document.getDocumentElement().getNodeName())) {
        throw new IllegalStateException("Invalid root element '" + document.getDocumentElement().getNodeName()
                + "', expected '" + expectedRootElementName + "'");

    }
}

From source file:Main.java

/**
 * @param doc/*  www. j ava  2  s .c  o m*/
 * @return
 */
public static Element getImplicitElementFromDoc(Document doc) {
    // FIXME: Handle "first topic child of root" rule for non-topic elements.
    return doc.getDocumentElement();
}

From source file:Main.java

/**
 * get first "real" element (ie not the document-rootelement, but the next one
 *//*from   www.j  a  va 2s. c om*/
public static Element getFirstElement(Document document) {
    Element workelement = null;

    if (document.getDocumentElement() != null) {
        org.w3c.dom.Node tmp = document.getDocumentElement().getFirstChild();

        while (tmp != null) {
            tmp = tmp.getNextSibling();

            if (tmp.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                workelement = (Element) tmp;

                break;
            }
        }
    }

    return workelement;
}

From source file:Main.java

public static String readProjectName(String file) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    InputStream is = new FileInputStream(file);
    Document doc = dombuilder.parse(is);
    Element root = doc.getDocumentElement();
    NodeList prjInfo = root.getChildNodes();
    if (prjInfo != null) {
        for (int i = 0; i < prjInfo.getLength(); i++) {
            Node project = prjInfo.item(i);
            if (project.getNodeType() == Node.ELEMENT_NODE) {
                String strProject = project.getNodeName();
                if (strProject.equals("project")) {
                    for (Node node = project.getFirstChild(); node != null; node = node.getNextSibling()) {
                        if (node.getNodeType() == Node.ELEMENT_NODE) {
                            String strNodeName = node.getNodeName();
                            if (strNodeName.equals("name")) {
                                return node.getTextContent();
                            }/*from   w  w w  . ja  va 2 s  .c  om*/
                        }
                    }
                }
            }

        }
    }
    return "";
}

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/*from ww w . jav a  2s . co m*/
 *
 * @return a DOM Element
 *
 * @throws Exception if unable to parse the String or if it doesn't contain valid XML.
 */
public static Element elementFromString(String xml) throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(bais);
    bais.close();
    return document.getDocumentElement();
}

From source file:Main.java

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

From source file:Main.java

/**
 * @param target/* w  w w.  j av  a2s.  com*/
 * @param node
 * @return
 * @throws Exception
 */
public static Document copyDocument(Document document) throws Exception {
    Node n = document.getDocumentElement().cloneNode(true);
    Document result = newDocument();
    result.adoptNode(n);
    result.appendChild(n);
    return result;
}

From source file:Main.java

public static String documentToString(Document document) throws TransformerException {
    Node rootNode = (Node) document.getDocumentElement();
    Source source = new DOMSource(rootNode);

    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.transform(source, result);

    return stringWriter.getBuffer().toString().replace("\n", "");
}