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[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.parse("sample.xml");
    Element tree = document.getDocumentElement();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File fXmlFile = new File("data.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("Employee");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        System.out.println(nodeToString(nList.item(temp)));
    }//from   ww w  .  j a va2 s .co  m
}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new File("data.xml"));
    doc.getDocumentElement().normalize();

    System.out.println("City: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild()
            .getChildNodes().item(0).getAttributes().getNamedItem("data").getNodeValue());

    System.out.println("Postal Code: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild()
            .getChildNodes().item(1).getAttributes().getNamedItem("data").getNodeValue());

    System.out.println("Date: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild()
            .getChildNodes().item(4).getAttributes().getNamedItem("data").getNodeValue());
}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    File myfile = new File("data.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(myfile);
    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("test");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            System.out.println("First Name : " + getTagValue("id", eElement));
            System.out.println("Last Name : " + getTagValue("result", eElement));
        }//from w w w  . ja  va  2 s . c  o  m
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    File stocks = new File("Stocks.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(stocks);
    doc.getDocumentElement().normalize();

    System.out.println(doc.getDocumentElement().getNodeName());
    NodeList nodes = doc.getElementsByTagName("stock");

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            System.out.println("Stock Symbol: " + getValue("symbol", element));
            System.out.println("Stock Price: " + getValue("price", element));
            System.out.println("Stock Quantity: " + getValue("quantity", element));
        }//from   w ww  . j av a  2 s .  c om
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String s = "<p>" + "  <media type='audio' id='au008093' rights='aaa'>" + "    <title>title</title>"
            + "  this is a test." + "</p>";
    InputStream is = new ByteArrayInputStream(s.getBytes());

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document d = db.parse(is);

    Node rootElement = d.getDocumentElement();
    System.out.println(nodeToString(rootElement));
}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    File fXmlFile = new File("data.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("staff");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        System.out.println("\nCurrent Element :" + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            System.out.println("Staff id : " + eElement.getAttribute("id"));
            System.out.println(/* w  w w  .  j a  va 2  s .c o m*/
                    "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
            System.out.println(
                    "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
            System.out.println(
                    "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
            System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuild = dbf.newDocumentBuilder();
    Document doc = docBuild.parse(new File("test2.xml"));

    Element x = doc.getDocumentElement();
    NodeList m = x.getChildNodes();
    for (int i = 0; i < m.getLength(); i++) {
        Node it = m.item(i);// ww w .  java  2 s  .co  m
        if (it.getNodeType() == 3) {
            System.out.println(it.getNodeValue());
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileInputStream fileInputStream = new FileInputStream(new File("src/file.xml"));
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document doc1 = builder.parse(fileInputStream);
    doc1.getDocumentElement().normalize();
    NodeList kList1 = doc1.getElementsByTagName("item");

    StringBuilder stringBuilder = new StringBuilder();

    for (int temp = 0; temp < kList1.getLength(); temp++) {
        Node kNode1 = kList1.item(temp);
        System.out.println("\nCurrent Element :" + kNode1.getNodeName());
        if (kNode1.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) kNode1;
            System.out.println("node name" + eElement.getNodeName());
            Node in = eElement.getFirstChild();
            if ((in.getTextContent() != null) && !(in.getTextContent()).isEmpty()
                    && !(in.getTextContent().length() == 0))
                stringBuilder.append(in.getTextContent());
        }//  w  w w  .  j av  a 2s .c om
    }
    System.out.println(stringBuilder);
}

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 order = document.getDocumentElement();

    DocumentRange ranges = (DocumentRange) document;

    Range range = ranges.createRange();/*ww w  .j  a v a 2s .c  o  m*/
    range.setStartBefore(order.getFirstChild());
    range.setEndAfter(order.getLastChild());

    range.deleteContents();
    range.detach();

}