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();
    factory.setNamespaceAware(true);/* w  w w.j  a v  a2 s.  co m*/
    factory.setValidating(true);

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

    String docNS = "http://www.my-company.com";

    Element order = document.getDocumentElement();
    Attr date = order.getAttributeNodeNS(docNS, "date");
    NodeList children = order.getElementsByTagNameNS(docNS, "item");

}

From source file:Main.java

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

    removeEmptyChildElements(doc.getDocumentElement());

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("clean.xml"));
    transformer.transform(source, result);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse("server.xml");
    Element root = document.getDocumentElement();
    Element rootElement = document.getDocumentElement();

    Collection<Server> svr = new ArrayList<Server>();
    svr.add(new Server());

    for (Server i : svr) {
        Element server = document.createElement("server");
        rootElement.appendChild(server);

        Element name = document.createElement("name");
        name.appendChild(document.createTextNode(i.getName()));
        server.appendChild(name);// ww w . j  ava 2s.c  o  m

        Element port = document.createElement("port");
        port.appendChild(document.createTextNode(Integer.toString(i.getPort())));
        server.appendChild(port);

        root.appendChild(server);
    }

    DOMSource source = new DOMSource(document);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StreamResult result = new StreamResult("server.xml");
    transformer.transform(source, result);
}

From source file:Main.java

License:asdf

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

    dom(doc.getDocumentElement());

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

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new InputSource(new StringReader(cfgXml)));
    doc.getDocumentElement().normalize();

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

    NodeList nList = doc.getElementsByTagName("config");
    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;
            Config c = new Config();
            c.name = eElement.getAttribute("name");
            c.type = eElement.getAttribute("type");
            c.format = eElement.getAttribute("format");
            c.size = Integer.valueOf(eElement.getAttribute("size"));
            c.scale = Integer.valueOf(eElement.getAttribute("scale"));
            String attribute = eElement.getAttribute("required");
            c.required = Boolean.valueOf("Yes".equalsIgnoreCase(attribute) ? true : false);
            System.out.println("Imported config : " + c);
        }// w w  w .  ja va2s.co  m
    }
}

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

    addText(doc.getDocumentElement(), "new text");

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

    setAttribute(doc.getDocumentElement(), "attr", "new Vaue");

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

    removeAttribute(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();
    factory.setValidating(true);/*from w  w w . j a  va2s  .  com*/

    factory.setExpandEntityReferences(false);

    Document doc1 = factory.newDocumentBuilder().parse(new File("filename"));
    NodeList list = doc1.getElementsByTagName("entry");
    Element element = (Element) list.item(0);

    Document doc2 = factory.newDocumentBuilder().parse(new File("infilename2.xml"));

    Node dup = doc2.importNode(element, true);

    doc2.getDocumentElement().appendChild(dup);
}

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

    createElement(doc.getDocumentElement(), "new");

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

}