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

/**
 * Return the root node of a XML document.
 * @param fileNameXML name of the XML file
 * @throws ParserConfigurationException//from   w  w w  . j  a v  a  2 s.c  om
 * @throws SAXException
 * @throws IOException
 */
public static Document getXMLDocument(String fileNameXML)
        throws ParserConfigurationException, SAXException, IOException {
    File file = new File(fileNameXML);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    return doc;
}

From source file:Main.java

public static String xpath(String xml, String xpath) throws Exception {
    XPath xp = XPathFactory.newInstance().newXPath();
    Document doc = parse(xml);
    return xp.evaluate(xpath, doc.getDocumentElement());
}

From source file:Main.java

/**
 * returns a XML node list./*from  ww  w .  ja  va2  s .  c  o  m*/
 *
 * @param pDocument         Document XML DOM document
 * @param psTagName         String XML node name
 * @return                  NodeList XML node list
 */
public static org.w3c.dom.NodeList getNodeList(Document pDocument, String psTagName) {
    return pDocument.getDocumentElement().getElementsByTagName(psTagName);
}

From source file:Main.java

/**
 * Build a Document from a file//www .j a  va 2  s .c om
 *
 * @param xmlFile file
 * @return a Document object
 * @throws Exception if an error occurs
 */
public static Document buildDocumentFromFile(File xmlFile) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);
    doc.getDocumentElement().normalize();
    return doc;
}

From source file:Main.java

public static Document createDocument(Document document) {
    return createDocument(document.getDocumentElement());
}

From source file:Main.java

public static String getNamespaceURI(final Document doc, final 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());
        }//  www  . j  a v a 2  s  . co  m
    }
    return null;
}

From source file:Utils.java

/**
 * Copy an XML document, adding it as a child of the target document root
 * @param source Document to copy//from w  w w  . j av a  2s  .  c  o  m
 * @param target Document to contain copy
 */
public static void copyDocument(Document source, Document target) {
    Node node = target.importNode(source.getDocumentElement(), true);

    target.getDocumentElement().appendChild(node);
}

From source file:Main.java

private static void cleanDocument(final Document document) throws XPathExpressionException {
    document.getDocumentElement().normalize();
    removeEmptyTextNodes(document);//ww  w  .  j ava  2s .  co m
}

From source file:Main.java

static String getXMLString(Document xmlDoc) throws Exception {
    StringWriter writer = null;/*from  w  w w  .  java 2  s  .  co m*/
    DOMSource source = new DOMSource(xmlDoc.getDocumentElement());
    writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.transform(source, result);
    StringBuffer strBuf = writer.getBuffer();
    return strBuf.toString();
}

From source file:Main.java

private static void makeNamelist(Document doc) {
    String names = null;/*  w w w.  j a  va 2  s.  co m*/
    Element root = doc.getDocumentElement();
    NodeList nameElements = root.getElementsByTagName("name");
    for (int i = 0; i < nameElements.getLength(); i++) {
        Element name = (Element) nameElements.item(i);
        Text nametext = (Text) name.getFirstChild();
        if (names == null) {
            names = nametext.getData();
        } else {
            names += ", " + nametext.getData();
        }
    }
    Element namelist = doc.createElement("names");
    Text namelisttext = doc.createTextNode(names);
    namelist.appendChild(namelisttext);
    root.insertBefore(namelist, root.getFirstChild());
}