22

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>
M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
zoma.saf
  • 558
  • 3
  • 6
  • 14
  • 4
    Read this tutorial http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/ – constantlearner Nov 12 '13 at 21:12
  • And this http://www.beingjavaguys.com/2013/06/dom-xml-parser-in-java.html – constantlearner Nov 12 '13 at 21:13
  • Java's standard DOM API is a pain in the butt. Consider using a friendlier library like one of those mentioned in http://stackoverflow.com/questions/831865/what-java-xml-library-do-you-recommend-to-replace-dom4j – dnault Nov 12 '13 at 21:15
  • 1
    these tutorials are for reading from xml file, I don't want to read xml file. I want to construct xml file grammatically. – zoma.saf Nov 12 '13 at 21:15

1 Answers1

27

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();
        }
    }
}
M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
Bizmarck
  • 2,663
  • 2
  • 33
  • 48
  • thx a lot for the big help, is there also anyway to convert String tags=" 2013 11 14 "; intoxml file directly ? – zoma.saf Nov 12 '13 at 22:23
  • 1
    Not that I'm aware. One approach would be to split them and iterate through the keys/values, calling `createElement` and `appendChild` for keys and values respectively. – Bizmarck Nov 14 '13 at 14:09
  • It's not a good idea to use an internal implementation class; it may not be available when using a different JDK. Best to stick with public API. – gerardw Jan 12 '18 at 14:55