Create XML file with DOM
- Read a XML file with DOM
- Create XML file with DOM
- Delete XML nodes with DOM
- Read a XML file with DOM
- Create XML file with DOM
- Delete XML nodes with DOM
- Read a XML file with SAX
- JAX-B Marshal
- JAX-B Unmarshal
- Convert properties file into XML file
- Convert XML file into properties file
- XPath tutorial
- From XML to JSON
Comments (0)
Here's an example of how to create an XML file in java using DOM parser.
Java Code Java Code
Create a XML Document with DOM.
package com.camelcode;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
public class CreateXml {
public static void main(String...args){
try {
String[] categories = {"COOKING", "CHILDREN", "WEB"};
String[] titles = {"Everyday Italian", "Harry Potter", "Learning XML"};
String[] authors = {"Giada De Laurentiis", "J K. Rowling", "Erik T. Ray"};
Integer[] years = {2005, 2005, 2003};
Double[] prices = {30.00, 29.99, 39.95};
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element root = document.createElement("bookstore");
document.appendChild(root);
Comment comment = document.createComment("Comment");
root.appendChild(comment);
for (int i = 0; i < 3; i++){
Element child = document.createElement("book");
child.setAttribute("category", categories[i]);
root.appendChild(child);
Element title = document.createElement("title");
Text titleText = document.createTextNode(titles[i]);
title.appendChild(titleText);
child.appendChild(title);
Element author = document.createElement("author");
Text authorText = document.createTextNode(authors[i]);
author.appendChild(authorText);
child.appendChild(author);
Element year = document.createElement("year");
Text yearText = document.createTextNode(years[i].toString());
year.appendChild(yearText);
child.appendChild(year);
Element price = document.createElement("price");
Text priceText = document.createTextNode(prices[i].toString());
price.appendChild(priceText);
child.appendChild(price);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("books.xml"));
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (Exception e){
e.printStackTrace();
}
}
}
Output result
The file that's been created: Books.xml





Latest Posts
Latest Comments
Tag cloud