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 void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document originalDocument = db.parse(new File("input.xml"));
    Node originalRoot = originalDocument.getDocumentElement();

    Document copiedDocument = db.newDocument();
    Node copiedRoot = copiedDocument.importNode(originalRoot, true);
    copiedDocument.appendChild(copiedRoot);

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    Document doc = docBuilder.parse(new FileInputStream("data.xml"));
    Element root = doc.getDocumentElement();
    org.w3c.dom.NodeList nodeList = root.getElementsByTagName("key");
    for (int i = 0; i < nodeList.getLength(); i++) {
        System.out.print(((Node) nodeList.item(i)).getAttributes().getNamedItem("keyname"));
        System.out.println("\tvalue: " + ((Node) nodeList.item(i)).getTextContent());
    }/*from w w w  .  j ava2s  .  c  om*/

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("test.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document oldDoc = builder.parse(is);
    Node oldRoot = oldDoc.getDocumentElement();
    Document newDoc = builder.newDocument();
    Element newRoot = newDoc.createElement("newroot");
    newDoc.appendChild(newRoot);//  w w w.  j a  v a2s .c om
    newRoot.appendChild(newDoc.importNode(oldRoot, true));

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DOMSource domSource = new DOMSource(newDoc);
    Writer writer = new OutputStreamWriter(out);
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    writer.flush();

    InputStream isNewXML = new ByteArrayInputStream(out.toByteArray());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.parse("sample.xml");

    Element purchaseOrder = document.getDocumentElement();

    Attr orderDate = purchaseOrder.getAttributeNode("date");
    System.out.println(orderDate.getValue());

    NamedNodeMap attrs = purchaseOrder.getAttributes();
    int attrsCount = attrs.getLength();

    for (int i = 0; i < attrsCount; i++) {
        Attr item = (Attr) attrs.item(i);
        System.out.println("'" + item.getName() + "' = '" + item.getValue() + "'");
    }/* www  . ja v a  2 s  . c o  m*/
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from   w  w w.  ja v a2s .  c  o  m*/
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse("yourFile.xml");
    Element rootElement = doc.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    Node current = null;
    for (int i = 0; i < children.getLength(); i++) {
        current = children.item(i);
        if (current.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) current;
            if (element.getTagName().equalsIgnoreCase("tableOfContents")) {
                rootElement.removeChild(element);
            }
        }
    }

    System.out.println(doc.getDocumentElement());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document dom = db.parse("data.xml");
    Element docEle = dom.getDocumentElement();
    NodeList nl = docEle.getElementsByTagName("staff");

    if (nl != null && nl.getLength() > 0) {
        for (int i = 0; i < nl.getLength(); i++) {
            // get the employee element
            Element el = (Element) nl.item(i);
            String firstname = getTextValue(el, "firstname");
            String lastname = getTextValue(el, "lastname");
            String nickname = getTextValue(el, "nickname");
            int salary = getIntValue(el, "salary");

            System.out.println(firstname);
            System.out.println(lastname);
            System.out.println(nickname);
            System.out.println(salary);
        }//from w ww . java2 s.c o m
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*www  . j  a  va2 s.  co  m*/
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse("yourFile.xml");
    Element rootElement = doc.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    Node current = null;
    int count = children.getLength();
    for (int i = 0; i < count; i++) {
        current = children.item(i);
        if (current.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) current;
            if (element.getTagName().equalsIgnoreCase("tableOfContents")) {
                element.setAttribute("showPageNumbers", "no");
            }
        }
    }

    System.out.println(doc.getDocumentElement());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/* w ww.  jav  a  2s . co m*/

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element element = doc.getDocumentElement();
    Comment comment = doc.createComment("A Document Comment");
    element.getParentNode().insertBefore(comment, element);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//  w w  w  .j a  v  a2s  .  c o  m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element element = doc.getDocumentElement();

    Text text1 = (Text) element.getFirstChild();
    String string = text1.getData();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();
    Document document = loader.parse("sample.xml");
    Element purchaseOrder = document.getDocumentElement();
    printElement(purchaseOrder, "");
}