Lets Assume we want to create an XML file,
Eg:-
CreatXMLFile.java
----------------------
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
public class CreatXMLFile
{
public static void main(String[] args) throws Exception
{
//creating factory
DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();
//creating a builder
DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();
//creating a document
Document document = documentBuilder.newDocument();
String root = "SMS";
//adding root Element
Element rootElement = document.createElement(root);
rootElement.setAttribute("area", "hyderabad");
document.appendChild(rootElement); //adds <SMS> as root element
String element ="Number";
String data = "12345";
Element em = document.createElement(element);
em.appendChild(document.createTextNode(data));
rootElement.appendChild(em); //creates <Number>12345</Number> and adds to root element<SMS>
/*
<SMS>
<Number>12345</Number>
</SMS>
*/
element ="message";
data= "hello";
em = document.createElement(element);
em.appendChild(document.createTextNode(data));
rootElement.appendChild(em); //creates <message>hello</message> and adds to root element<SMS>
/*
<SMS>
<Number>12345</Number>
<message>hello</message>
</SMS>
*/
em=document.createElement("delivered");
Element childElement = document.createElement("status");
childElement.appendChild(document.createTextNode("failed"));
em.appendChild(childElement);
rootElement.appendChild(em);//creates <delivered><status> failed</status></delivered> and adds to root element<SMS>
/*<SMS>
<Number>12345</Number>
<message>hello</message>
<delivered>
<status> failed</status>
</delivered>
</SMS>
*/
//Used to Write the generated xml structure to file or system console
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");//arrange the xml in an idented format
DOMSource source = new DOMSource(document);
File f=new File("d:/myfile.xml");
StreamResult result = new StreamResult(f); //prints xml into file
//StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
}
}
Eg:-
CreatXMLFile.java
----------------------
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
public class CreatXMLFile
{
public static void main(String[] args) throws Exception
{
//creating factory
DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();
//creating a builder
DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();
//creating a document
Document document = documentBuilder.newDocument();
String root = "SMS";
//adding root Element
Element rootElement = document.createElement(root);
rootElement.setAttribute("area", "hyderabad");
document.appendChild(rootElement); //adds <SMS> as root element
String element ="Number";
String data = "12345";
Element em = document.createElement(element);
em.appendChild(document.createTextNode(data));
rootElement.appendChild(em); //creates <Number>12345</Number> and adds to root element<SMS>
/*
<SMS>
<Number>12345</Number>
</SMS>
*/
element ="message";
data= "hello";
em = document.createElement(element);
em.appendChild(document.createTextNode(data));
rootElement.appendChild(em); //creates <message>hello</message> and adds to root element<SMS>
/*
<SMS>
<Number>12345</Number>
<message>hello</message>
</SMS>
*/
em=document.createElement("delivered");
Element childElement = document.createElement("status");
childElement.appendChild(document.createTextNode("failed"));
em.appendChild(childElement);
rootElement.appendChild(em);//creates <delivered><status> failed</status></delivered> and adds to root element<SMS>
/*<SMS>
<Number>12345</Number>
<message>hello</message>
<delivered>
<status> failed</status>
</delivered>
</SMS>
*/
//Used to Write the generated xml structure to file or system console
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");//arrange the xml in an idented format
DOMSource source = new DOMSource(document);
File f=new File("d:/myfile.xml");
StreamResult result = new StreamResult(f); //prints xml into file
//StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
}
}
No comments:
Post a Comment