See the following sample class:
GenerateXML.java
package opensourzesupport.xml
import java.io.PrintStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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;
/**
*
* @author Prasobh.K
*/
public class GenerateXML {
public static void main(String[] args) {
String documentToBeCreated = "myXMLDocument.xml";
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// creating a new document
Document myXMLDocument = builder.newDocument();
//creating a root element
Element rootElement = myXMLDocument.createElement("root");
myXMLDocument.appendChild(rootElement);
for (int i = 1; i <4; i++) {
//creating a child root element
Element childRootElement = myXMLDocument.createElement("childroot" + i);
rootElement.appendChild(childRootElement);
for (int j = 1; j < 10; j++) {
Element child = myXMLDocument.createElement("innerelement");
child.setTextContent("" + j);
childRootElement.appendChild(child);
}
}
// The XML document we created above is still in memory
// so we have to output it to a real file.
// In order to do it we first have to create
// an instance of DOMSource
DOMSource source = new DOMSource(myXMLDocument);
// PrintStream will be responsible for writing
// the text data to the file
PrintStream ps = new PrintStream(documentToBeCreated);
StreamResult result = new StreamResult(ps);
// Transformer instance, which we use to output the XML
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// The actual output to a file goes here
transformer.transform(source, result);
System.out.println("Sucessfully created " + documentToBeCreated);
} catch (Exception e) {
System.out.println("Failed tro create xml" + e);
}
}
}
Output :
myXMLDocument.xml
GenerateXML.java
package opensourzesupport.xml
import java.io.PrintStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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;
/**
*
* @author Prasobh.K
*/
public class GenerateXML {
public static void main(String[] args) {
String documentToBeCreated = "myXMLDocument.xml";
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// creating a new document
Document myXMLDocument = builder.newDocument();
//creating a root element
Element rootElement = myXMLDocument.createElement("root");
myXMLDocument.appendChild(rootElement);
for (int i = 1; i <4; i++) {
//creating a child root element
Element childRootElement = myXMLDocument.createElement("childroot" + i);
rootElement.appendChild(childRootElement);
for (int j = 1; j < 10; j++) {
Element child = myXMLDocument.createElement("innerelement");
child.setTextContent("" + j);
childRootElement.appendChild(child);
}
}
// The XML document we created above is still in memory
// so we have to output it to a real file.
// In order to do it we first have to create
// an instance of DOMSource
DOMSource source = new DOMSource(myXMLDocument);
// PrintStream will be responsible for writing
// the text data to the file
PrintStream ps = new PrintStream(documentToBeCreated);
StreamResult result = new StreamResult(ps);
// Transformer instance, which we use to output the XML
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// The actual output to a file goes here
transformer.transform(source, result);
System.out.println("Sucessfully created " + documentToBeCreated);
} catch (Exception e) {
System.out.println("Failed tro create xml" + e);
}
}
}
Output :
myXMLDocument.xml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<innerelement>1</innerelement>
<innerelement>2</innerelement>
<innerelement>3</innerelement>
<innerelement>4</innerelement>
<innerelement>5</innerelement>
<innerelement>6</innerelement>
<innerelement>7</innerelement>
<innerelement>8</innerelement>
<innerelement>9</innerelement>
</childroot1>
<innerelement>1</innerelement>
<innerelement>2</innerelement>
<innerelement>3</innerelement>
<innerelement>4</innerelement>
<innerelement>5</innerelement>
<innerelement>6</innerelement>
<innerelement>7</innerelement>
<innerelement>8</innerelement>
<innerelement>9</innerelement>
</childroot2>
<innerelement>1</innerelement>
<innerelement>2</innerelement>
<innerelement>3</innerelement>
<innerelement>4</innerelement>
<innerelement>5</innerelement>
<innerelement>6</innerelement>
<innerelement>7</innerelement>
<innerelement>8</innerelement>
<innerelement>9</innerelement>
</childroot3>
</root>
No comments:
Post a Comment