Create new xml file
step:
1. Get the factory instance DocumentBuilderFactory of the DOM parser
2. Get the DOM parser from the DOM factory
3 Create a DOM tree
4 Create a node, add a node
5 Add the node you just created to another node
6 Add the node to the document
7 Save the xml file
Code:
// 1. Get the factory instance of the DOM parser DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2. Get the DOM parser from the DOM factory DocumentBuilder db; try { db = dbf.newDocumentBuilder(); Document docs = db.newDocument(); //create node Element noteElement = docs.createElement("note"); Element toElement = docs.createElement("to"); Text l = docs.createTextNode("333"); toElement.appendChild(l);//lableElement.setNodeValue(lable); Element fromElement = docs.createElement("from"); Text t = docs.createTextNode("321"); fromElement.appendChild(t);//titleElement.setNodeValue(title); Element headingElement = docs.createElement("heading"); Text x = docs.createTextNode("sadda"); headingElement.appendChild(x);//textElement.setNodeValue(text); String time=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()); Element bodyElement = docs.createElement("body"); Text m = docs.createTextNode(time); bodyElement.appendChild(m); //Add parent-child relationship noteElement.appendChild(toElement); noteElement.appendChild(fromElement); noteElement.appendChild(headingElement); noteElement.appendChild(bodyElement); //Element docsElement=(Element)docs.getElementsByTagName("docs").item(0); //docsElement.appendChild(docElement); //Add the node to the document docs.getDocumentElement().appendChild(noteElement); //save the xml file TransformerFactory transformerFactory=TransformerFactory.newInstance(); Transformer transformer=transformerFactory.newTransformer(); DOMSource domSource=new DOMSource(docs); //set encoding type transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); File f=new File("e:\\Test.xml"); StreamResult result=new StreamResult(new FileOutputStream(f)); //Convert DOM tree to xml file transformer.transform(domSource, result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace (); } }
Additional content:
package com.test; import java.io.File; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; 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 org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; public class TestXml { public static void main(String[] args) { // 1. Get the factory instance of the DOM parser DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2. Get the DOM parser from the DOM factory DocumentBuilder db; try { db = dbf.newDocumentBuilder(); // 3. Parse the XML document to get the DOM tree File f=new File("e:\\Test.xml"); Document docs = db.parse(f); //create node Element noteElement = docs.createElement("note"); Element toElement = docs.createElement("to"); Text l = docs.createTextNode("333"); toElement.appendChild(l);//lableElement.setNodeValue(lable); Element fromElement = docs.createElement("from"); Text t = docs.createTextNode("321"); fromElement.appendChild(t);//titleElement.setNodeValue(title); Element headingElement = docs.createElement("heading"); Text x = docs.createTextNode("sadda"); headingElement.appendChild(x);//textElement.setNodeValue(text); String time=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()); Element bodyElement = docs.createElement("body"); Text m = docs.createTextNode(time); bodyElement.appendChild(m); //Add parent-child relationship noteElement.appendChild(toElement); noteElement.appendChild(fromElement); noteElement.appendChild(headingElement); noteElement.appendChild(bodyElement); docs.getDocumentElement().appendChild(noteElement); //save the xml file TransformerFactory transformerFactory=TransformerFactory.newInstance(); Transformer transformer=transformerFactory.newTransformer(); DOMSource domSource=new DOMSource(docs); //set encoding type transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result=new StreamResult(new FileOutputStream(f)); //Convert DOM tree to xml file transformer.transform(domSource, result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace (); } } }