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 IOException, SAXException, ParserConfigurationException {

    String xml = "<T rn='0'>"
            + "<I><P>UNKNOWN</P><K>UNKNOWN</K><N><O>UNKNOWN</O><P>UNKNOWN</P><U>UNKNOWN</U><N>UNKNOWN</N></N></I></T>";

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));

    print(doc.getDocumentElement(), "");
}

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 e = document.getDocumentElement();
    System.out.println(e);//from w w  w  . java  2  s . c  om
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from  w w w . j av a2  s .  c  o  m*/
    DocumentBuilder db = dbf.newDocumentBuilder();

    File file1 = new File("input1.xml");
    Document doc1 = db.parse(file1);
    Element rootElement1 = doc1.getDocumentElement();

    File file2 = new File("input2.xml");
    Document doc2 = db.parse(file2);
    Element rootElement2 = doc2.getDocumentElement();

    // Copy Child Nodes
    NodeList childNodes2 = rootElement2.getChildNodes();
    for (int x = 0; x < childNodes2.getLength(); x++) {
        Node importedNode = doc1.importNode(childNodes2.item(x), true);
        if (importedNode.getNodeType() == Node.ELEMENT_NODE) {
            Element importedElement = (Element) importedNode;
            // Copy Attributes
            NamedNodeMap namedNodeMap2 = rootElement2.getAttributes();
            for (int y = 0; y < namedNodeMap2.getLength(); y++) {
                Attr importedAttr = (Attr) doc1.importNode(namedNodeMap2.item(y), true);
                importedElement.setAttributeNodeNS(importedAttr);
            }
        }
        rootElement1.appendChild(importedNode);
    }

    // Output Document
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    DOMSource source = new DOMSource(doc1);
    StreamResult result = new StreamResult(System.out);
    t.transform(source, result);
}

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

    Element root = doc.getDocumentElement();
    EntityReference eref = (EntityReference) root.getFirstChild();

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//  w w w.ja  va2s  .  com
    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")) {
                // Get the list of <tocEntry> items
                NodeList tocitems = element.getElementsByTagName("tocEntry");
                // Obtain a reference to the second one
                Node secondChild = tocitems.item(1);
                // Create a new <tocEntry> element
                Element newTOCItem = doc.createElement("tocEntry");
                // Create a new "Help" text node
                Text newText = doc.createTextNode("Help");
                // Make it a child of the new <tocEntry> element
                // <tocEntry>Help</tocEntry>
                newTOCItem.appendChild(newText);
                // Add the new <tocEntry> element to <tableOfContents>
                element.insertBefore(newTOCItem, secondChild);
            }
        }
    }

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

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

    SOAPHeader soapHeader = soapEnvelope.getHeader();
    SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName("Signature",
            "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));

    SOAPBody soapBody = soapEnvelope.getBody();
    soapBody.addAttribute(/* w w w  .ja va 2s .c  o m*/
            soapEnvelope.createName("id", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"),
            "Body");
    Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.com");
    SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);

    Source source = soapPart.getContent();

    Node root = null;
    if (source instanceof DOMSource) {
        root = ((DOMSource) source).getNode();
    } else if (source instanceof SAXSource) {
        InputSource inSource = ((SAXSource) source).getInputSource();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = null;

        db = dbf.newDocumentBuilder();

        Document doc = db.parse(inSource);
        root = (Node) doc.getDocumentElement();
    }

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(new DOMSource(root), new StreamResult(System.out));
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document xmldoc = impl.createDocument(null, "XMLDoc", null);

    Element root = xmldoc.getDocumentElement();
    Element e0 = xmldoc.createElement("Doc");
    Element e1 = xmldoc.createElement("TITLE");
    Node n1 = xmldoc.createTextNode("Java");
    e1.appendChild(n1);/*from w w  w  .  ja v a 2s  . c om*/

    Element e2 = xmldoc.createElement("data");
    Node n2 = xmldoc.createTextNode("text node");
    e2.appendChild(n2);

    e0.appendChild(e1);
    e0.appendChild(e2);
    root.appendChild(e0);

    StreamResult out = new StreamResult("howto.xml");
    DOMSource domSource = new DOMSource(xmldoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:ListMoviesXML.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);/* ww w. j  a va 2s.com*/

    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse(new InputSource("y.xml"));
    Element root = doc.getDocumentElement();

    Element movieElement = (Element) root.getFirstChild();
    Movie m;
    while (movieElement != null) {
        m = getMovie(movieElement);
        String msg = Integer.toString(m.year);
        msg += ": " + m.title;
        msg += " (" + m.price + ")";
        System.out.println(msg);
        movieElement = (Element) movieElement.getNextSibling();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document xmldoc = impl.createDocument(null, "TODOs", null);

    Element root = xmldoc.getDocumentElement();
    Element e0 = xmldoc.createElement("TOPIC");
    Element e1 = xmldoc.createElement("TITLE");
    Node n1 = xmldoc.createTextNode("Java");
    e1.appendChild(n1);/*from  w  w  w .j  a  v  a 2 s  .com*/

    Element e2 = xmldoc.createElement("URL");
    Node n2 = xmldoc.createTextNode("http://www.server.com");
    e2.appendChild(n2);
    e0.appendChild(e1);
    e0.appendChild(e2);
    root.appendChild(e0);

    Node pi = xmldoc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"TODOs.xsl\"");
    xmldoc.insertBefore(pi, root);

    StreamResult out = new StreamResult("howto.xml");
    DOMSource domSource = new DOMSource(xmldoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Car> carsList = new ArrayList<Car>();
    Set<Car> carsset = new HashSet<Car>();
    File fXmlFile = new File("cars.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();
    NodeList nList = doc.getElementsByTagName("cars");
    Car tempCar = null;//from ww w.  j a v  a 2 s  .  com
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            tempCar = new Car();
            Element eElement = (Element) nNode;
            tempCar.setModel(eElement.getElementsByTagName("model").item(0).getTextContent());
            tempCar.setVersion(eElement.getElementsByTagName("version").item(0).getTextContent());
            carsList.add(tempCar);
            carsset.add(tempCar);

        }
    }
    for (Car cs : carsset) {
        int count = 0;
        for (Car cl : carsList) {
            if (cs.equals(cl)) {
                count = count + 1;
            }
        }
        System.out.println(cs + "\t" + count);
    }
}