Step 1- Create document
Step 2- Create root element
Step 3- Append root into document
Step 4- Create child element of root element
Step 5- Append it to root element
Step 6- Create child element
Step 7- Create node text
Step 8- Append it to parent element
Repeat this for next child element.
Example -
I’m going to create below xml file-
WriteXMLFile.java
Output file :
myWriteXml.xml
Step 2- Create root element
Step 3- Append root into document
Step 4- Create child element of root element
Step 5- Append it to root element
Step 6- Create child element
Step 7- Create node text
Step 8- Append it to parent element
Repeat this for next child element.
Example -
I’m going to create below xml file-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0"?> <company> <staff id="1001"> <firstname>Ram</firstname> <lastname>Singh</lastname> <nickname>ram</nickname> <salary>100000</salary> </staff> <staff id="2001"> <firstname>Shyam</firstname> <lastname>Singh</lastname> <nickname>shyam</nickname> <salary>200000</salary> </staff> </company> |
WriteXMLFile.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package com. atozjavatutorials; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; public class WriteXMLFile { public static void main(String argv[]) { try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("company"); doc.appendChild(rootElement); // staff elements Element staff = doc.createElement("Staff"); rootElement.appendChild(staff); // set attribute to staff element Attr attr = doc.createAttribute("id"); attr.setValue("1"); staff.setAttributeNode(attr); // shorten way // staff.setAttribute("id", "1"); // firstname elements Element firstname = doc.createElement("firstname"); firstname.appendChild(doc.createTextNode("Ram")); staff.appendChild(firstname); // lastname elements Element lastname = doc.createElement("lastname"); lastname.appendChild(doc.createTextNode("Singh")); staff.appendChild(lastname); // nickname elements Element nickname = doc.createElement("nickname"); nickname.appendChild(doc.createTextNode("ram")); staff.appendChild(nickname); // salary elements Element salary = doc.createElement("salary"); salary.appendChild(doc.createTextNode("100000")); staff.appendChild(salary); // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("src\\myWriteXml.xml")); transformer.transform(source, result); System.out.println("File saved!"); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (TransformerException tfe) { tfe.printStackTrace(); } } } |
Output file :
myWriteXml.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0"?> <company> <staff id="1001"> <firstname>Ram</firstname> <lastname>Singh</lastname> <nickname>ram</nickname> <salary>100000</salary> </staff> <staff id="2001"> <firstname>Shyam</firstname> <lastname>Singh</lastname> <nickname>shyam</nickname> <salary>200000</salary> </staff> </company> |
No comments :
Post a Comment