How do you create XML nodes using Java DOM?

zoma.saf :

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>
Bizmarck :

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you create a for loop using a variable in javascript?

How do you create a REST client for Java?

How do I copy DOM nodes from one document to another in Java?

How do you create a PDF from XML in Java?

How do I remove namespaces from xml, using java dom?

How to add Doctype in XML document using DOM (JAVA)

How do you create a Java applet using JRuby?

Handling Empty Nodes Using Java DOM

How do you swap data between two nodes? (Java)

Creating namespace prefixed XML nodes in Java DOM

Using puppeteer how do you get all child nodes of a node?

How do I fold nodes of an XML element using lens?

How to create nodes to append when using XML::LibXML to write XML?

How do you create a form wizard using Blazor?

How do you comment out an XML node using Powershell 3.0?

How to rename the name of attribute in a node of xml in java using dom parser

How do you override a spring bean defined in xml using Annotations?

How do you get djangorestframework to return xml using format suffix?

How do you create objects using a for loop

parse xml using dom java

How to Read all nodes of xml using xpathh in java?

How to Parse XML without using DOM or SAX Parser in Java?

Create array from xml nodes using jquery

How do you send XML files using Azure Storage Queue?

How do you cycle through nodes in XML file

how do you retrieve xml data from nested nodes

How do you remove duplicate xml nodes throughout all of the xml python

How do you do a for each to create nodes for each element of a node property?

How to create a tree by using nodes that point to next brother in Java?