Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create XML nodes using Java DOM?

Tags:

How can I create the below XML using Java DOM, I want to create it from scratch. Is there any way? I don't want to read it and clone it, I just want to create it by DOM methods.

Java Example:

Node booking=new Node();
Node bookingID=new Node();
booking.add(bookingID);

XML Example:

<tns:booking>
    <tns:bookingID>115</tns:bookingID>
    <tns:type>double</tns:type>
    <tns:amount>1</tns:amount>
    <tns:stayPeriod>
        <tns:checkin>
            <tns:year>2013</tns:year>
            <tns:month>11</tns:month>
            <tns:date>14</tns:date>
        </tns:checkin>
        <tns:checkout>
            <tns:year>2013</tns:year>
            <tns:month>11</tns:month>
            <tns:date>16</tns:date>
        </tns:checkout>
    </tns:stayPeriod>
</tns:booking>
like image 870
zoma.saf Avatar asked Nov 12 '13 21:11

zoma.saf


People also ask

What is a node in XML DOM?

According to the XML DOM, everything in an XML document is a node: The entire document is a document node. Every XML element is an element node. The text in the XML elements are text nodes. Every attribute is an attribute node.

How to create a new element Node from an XML document?

("tagname") − is the name of new element node to be created. The following example (createnewelement_example.htm) parses an XML document ( node.xml) into an XML DOM object and creates a new element node PhoneNo in the XML document. new_element = xmlDoc.createElement ("PhoneNo"); creates the new element node <PhoneNo>

What is domdom in Java?

DOM stands for D ocument O bject M odel. A DOM is a standard tree structure to define the XML structure. The two most common types of nodes are element nodes and text nodes. The example contains few important elements: create a Document object using DocumentBuilder and DocumentBuilderFactory classes.

What is the name of new element Node created?

("tagname") − is the name of new element node to be created. The following example (createnewelement_example.htm) parses an XML document ( node.xml) into an XML DOM object and creates a new element node PhoneNo in the XML document.


1 Answers

Besides the tutorials mentioned already, here is a simple example that uses javax.xml.transform and org.w3c.dom packages:

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import com.sun.org.apache.xerces.internal.dom.DocumentImpl;

public class XML {
    public static void main(String[] args) {
        XML xml = new XML();
        xml.makeFile();
    }

    public void makeFile() {
        Node item = null;
        Document xmlDoc = new DocumentImpl();
        Element root = xmlDoc.createElement("booking");
        item = xmlDoc.createElement("bookingID");
        item.appendChild(xmlDoc.createTextNode("115"));
        root.appendChild(item);
        xmlDoc.appendChild(root);

        try {
            Source source = new DOMSource(xmlDoc);
            File xmlFile = new File("yourFile.xml");
            StreamResult result = new StreamResult(new OutputStreamWriter(
                                  new FileOutputStream(xmlFile), "ISO-8859-1"));
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.transform(source, result);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
like image 152
Bizmarck Avatar answered Sep 19 '22 06:09

Bizmarck