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();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    printAttributes(doc.getDocumentElement());

    System.out.println(documentToString(doc));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//  ww w  .j  ava2  s  .  c  o  m

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

    String fragment = "<fragment>aaa</fragment>";

    factory = DocumentBuilderFactory.newInstance();
    Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(fragment)));

    Node node = doc.importNode(d.getDocumentElement(), true);

    DocumentFragment docfrag = doc.createDocumentFragment();

    while (node.hasChildNodes()) {
        docfrag.appendChild(node.removeChild(node.getFirstChild()));
    }

    Element element = doc.getDocumentElement();
    element.appendChild(docfrag);
}

From source file:TestDOM.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = factory.newDocumentBuilder();
    Document document = parser.parse("zooinventory.xml");
    Element inventory = document.getDocumentElement();
    NodeList animals = inventory.getElementsByTagName("Animal");

    System.out.println("Animals = ");
    for (int i = 0; i < animals.getLength(); i++) {
        String name = DOMUtil.getSimpleElementText((Element) animals.item(i), "Name");
        String species = DOMUtil.getSimpleElementText((Element) animals.item(i), "Species");
        System.out.println("  " + name + " (" + species + ")");
    }/*w  w  w.  j a  v  a  2 s. c  om*/

    Element foodRecipe = DOMUtil.getFirstElement((Element) animals.item(1), "FoodRecipe");
    String name = DOMUtil.getSimpleElementText(foodRecipe, "Name");
    System.out.println("Recipe = " + name);
    NodeList ingredients = foodRecipe.getElementsByTagName("Ingredient");
    for (int i = 0; i < ingredients.getLength(); i++)
        System.out.println("  " + DOMUtil.getSimpleElementText((Element) ingredients.item(i)));
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from   ww  w . j  a  v  a 2s.  com

    factory.setExpandEntityReferences(false);

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

    // Add a PI at the beginning of the document
    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction("target", "instruction");
    element.getParentNode().insertBefore(pi, element);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder()
            .parse("http://stackoverflow.com/feeds/tag?tagnames=java&sort=newest");
    Element root = doc.getDocumentElement();
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expression = xPath.compile("//entry");
    NodeList nl = (NodeList) expression.evaluate(root, XPathConstants.NODESET);
    System.out.println("Found " + nl.getLength() + " items...");
    for (int index = 0; index < nl.getLength(); index++) {
        Node node = nl.item(index);
        expression = xPath.compile("title");
        Node child = (Node) expression.evaluate(node, XPathConstants.NODE);
        System.out.println(child.getTextContent());
    }/*from  w  ww .ja v  a 2  s  . com*/
}

From source file:XMLInfo.java

public static void main(String args[]) {
    try {//w  w  w .  j av  a2 s.  c  om
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("xmlFileName.xml");
        Node root = document.getDocumentElement();
        System.out.print("Here is the document's root node:");
        System.out.println(" " + root.getNodeName());
        System.out.println("Here are its child elements: ");
        NodeList childNodes = root.getChildNodes();
        Node currentNode;

        for (int i = 0; i < childNodes.getLength(); i++) {
            currentNode = childNodes.item(i);
            System.out.println(currentNode.getNodeName());
        }

        // get first child of root element
        currentNode = root.getFirstChild();

        System.out.print("The first child of root node is: ");
        System.out.println(currentNode.getNodeName());

        // get next sibling of first child
        System.out.print("whose next sibling is: ");
        currentNode = currentNode.getNextSibling();
        System.out.println(currentNode.getNodeName());

        // print value of next sibling of first child
        System.out.println("value of " + currentNode.getNodeName() + " element is: "
                + currentNode.getFirstChild().getNodeValue());

        // print name of parent of next sibling of first child
        System.out.print("Parent node of " + currentNode.getNodeName() + " is: "
                + currentNode.getParentNode().getNodeName());
    }
    // handle exception creating DocumentBuilder
    catch (ParserConfigurationException parserError) {
        System.err.println("Parser Configuration Error");
        parserError.printStackTrace();
    }

    // handle exception reading data from file
    catch (IOException fileException) {
        System.err.println("File IO Error");
        fileException.printStackTrace();
    }

    // handle exception parsing XML document
    catch (SAXException parseException) {
        System.err.println("Error Parsing Document");
        parseException.printStackTrace();
    }
}

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

    Element purchaseOrder = document.getDocumentElement();
    printElement(purchaseOrder, "");
}

From source file:Main.java

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

    System.out.println(hasAttribute(doc.getDocumentElement(), "value"));

    System.out.println(documentToString(doc));

}

From source file:Main.java

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

    System.out.println(getAttributeValue(doc.getDocumentElement(), "attr"));

    System.out.println(documentToString(doc));

}

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_ELEMENT,
            null, true);//from ww w.  ja v a2s  .  c  om

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