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");

    DocumentTraversal traversal = (DocumentTraversal) document;

    TreeWalker walker = traversal.createTreeWalker(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null,
            true);// w  ww.  j  a  va2s.  co m

    traverseLevel(walker, "");
}

From source file:Main.java

public static void main(String[] args) throws Throwable {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    String entryType = null;//from w w w . j  a  v  a  2 s  .c  o m
    XPathExpression[] expressions = new XPathExpression[] {
            xpath.compile("local-name(*[local-name() = 'package'])"),
            xpath.compile("local-name(*[local-name() = 'libapp'])"),
            xpath.compile("local-name(*[local-name() = 'module'])") };

    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = fac.newDocumentBuilder();
    Document doc = parser.parse(args[0]);

    for (int i = 0; i < expressions.length; i++) {
        String found = (String) expressions[i].evaluate(doc.getDocumentElement(), XPathConstants.STRING);
        entryType = mappings.get(found);
        if (entryType != null && !entryType.trim().isEmpty()) {
            break;
        }
    }
    System.out.println(entryType);
}

From source file:DemoTreeWalker.java

public static void main(String args[]) throws SAXException, IOException {

    DOMParser parser = new DOMParser();
    parser.parse("games.xml");

    Document doc = parser.getDocument();

    DocumentTraversal docTraversal = (DocumentTraversal) doc;

    TreeWalker iter = docTraversal.createTreeWalker(doc.getDocumentElement(), NodeFilter.SHOW_ALL, null, false);
    Node n = null;//w ww  .  ja  v  a2 s.  c om
    while ((n = iter.nextNode()) != null) {
        System.out.println("Node name: " + n.getNodeName());
        System.out.println("Node value: " + n.getNodeValue());
    }

}

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");

    DocumentTraversal traversal = (DocumentTraversal) document;

    NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ALL,
            new ItemFilter(), true);

    for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
        System.out.println("Element: " + ((Element) n).getTagName());
    }/*from  w  w  w .j  av a 2s .  c o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<Services><service name='qwerty' id=''><File rootProfile='abcd' extension='acd'><Columns>"
            + "<name id='0' profileName='DATE' type='java'></name><name id='1' profileName='DATE' type='java'></name>"
            + "</Columns></File><File rootProfile='efg' extension='ghi'><Columns><name id='a' profileName='DATE' type='java'></name>"
            + "<name id='b' profileName='DATE' type='java'></name></Columns></File></service></Services>";
    DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;

    documentBuilder = documentFactory.newDocumentBuilder();

    org.w3c.dom.Document doc = documentBuilder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes())));

    doc.getDocumentElement().normalize();
    NodeList nodeList0 = doc.getElementsByTagName("service");
    NodeList nodeList1 = null;/*from w w w  .  j  ava2 s.co m*/
    NodeList nodeList2 = null;
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    for (int temp0 = 0; temp0 < nodeList0.getLength(); temp0++) {
        Node node0 = nodeList0.item(temp0);

        System.out.println("\nElement type :" + node0.getNodeName());
        Element Service = (Element) node0;

        if (node0.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        System.out.println("name : " + Service.getAttribute("name"));
        System.out.println("id : " + Service.getAttribute("id"));
        nodeList1 = Service.getChildNodes();
        for (int temp = 0; temp < nodeList1.getLength(); temp++) {
            Node node1 = nodeList1.item(temp);

            System.out.println("\nElement type :" + node1.getNodeName());

            Element File = (Element) node1;

            if (node1.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            System.out.println("rootProfile:" + File.getAttribute("rootProfile"));
            System.out.println("extension  : " + File.getAttribute("extension"));

            nodeList2 = File.getChildNodes();// colums
            for (int temp1 = 0; temp1 < nodeList2.getLength(); temp1++) {
                Element column = (Element) nodeList2.item(temp1);
                NodeList nodeList4 = column.getChildNodes();
                for (int temp3 = 0; temp3 < nodeList4.getLength(); temp3++) {
                    Element name = (Element) nodeList4.item(temp3);
                    if (name.getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    System.out.println("id:" + name.getAttribute("id"));
                    System.out.println("profileName  : " + name.getAttribute("profileName"));
                    System.out.println("type  : " + name.getAttribute("type"));
                }
            }
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    DocumentTraversal traversal = (DocumentTraversal) document;
    NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT,
            null, true);/* w  w w  . jav  a 2 s.c o m*/

    for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
        System.out.println("Element: " + ((Element) n).getTagName());
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    DocumentTraversal traversal = (DocumentTraversal) document;

    TreeWalker walker = traversal.createTreeWalker(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null,
            true);/*  www . jav a  2  s  .c  o m*/

    traverseLevel(walker, "");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    DocumentTraversal traversal = (DocumentTraversal) document;

    NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ALL,
            new ItemFilter(), true);

    for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
        System.out.println("Element: " + ((Element) n).getTagName());
    }/* ww w .ja va 2s  .  c  om*/
}

From source file:TryDOM.java

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

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        builderFactory.setValidating(true);
        builderFactory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder builder = null;
        builder = builderFactory.newDocumentBuilder();
        builder.setErrorHandler(new TryDOM());
        Document xmlDoc = builder.parse(new File("y.xml"));
        DocumentType doctype = xmlDoc.getDoctype();
        System.out.println("DOCTYPE node:\n" + getDoctypeString(doctype));
        listNodes(xmlDoc.getDocumentElement(), "");
    }//from w  w w .  j  a  va  2  s . co  m

From source file:Main.java

public static void main(String[] args) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document xmlDoc = docBuilder.parse(new File("sample.xml"));
    NodeList nodes = xmlDoc.getElementsByTagName("fr");
    for (int i = 0, length = nodes.getLength(); i < length; i++) {
        ((Element) nodes.item(i)).setTextContent("Modified");
    }/*from  w  w w  .  ja v a 2 s .  c  o  m*/
    xmlDoc.getDocumentElement().normalize();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    DOMSource domSource = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(new File("sample.xml"));
    transformer.transform(domSource, result);
    System.out.println("Modification Done");
}