Source: http://doraprojects.net/blog/creating-xml-file-in-java-from-array/
package com.blogspot.xml.sax;
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 javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
public class ReadXMLFile2 {
static String a;
static String b;
public static void main(String argv[]) {
try {
File fXmlFile = new File("C:\\Programy\\employees.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("record");
int size = nList.getLength();
String[][] Array = new String[2][size];
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
//System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
a = eElement.getElementsByTagName("date").item(0).getTextContent();
b = eElement.getElementsByTagName("value").item(0).getTextContent();
System.out.println("date : " + a);//eElement.getElementsByTagName("date").item(0).getTextContent());
System.out.println("value : " + b);// eElement.getElementsByTagName("value").item(0).getTextContent());
FileWriter out = new FileWriter ("C:\\Programy\\filename2.txt",true);
out.write(a + " " + b);
out.write("\r\n");
out.close();
Array[0][temp] = a;
Array[1][temp] = b;
}
}
for (int i = 0; i< size; i++)
//for(int j = 0; j < 2; j++)
{
System.out.println(Array[0][i] + " " + Array[1][i]);
}
XMLEncoder e = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("C:\\Programy\\Test2.xml")));
for (int i = 0; i< size; i++)
{
e.writeObject(Array[0][i]);
e.writeObject(Array[1][i]);
}
e.close();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc1 = builder.newDocument();
doc1.setXmlStandalone(true);
Element rootElement = doc1.createElement("record");
doc1.appendChild(rootElement);
for(int i=0; i<size;i++)
{
//Element personElement = doc1.createElement("id");
//personElement.setAttribute("id", "");
//rootElement.appendChild(personElement);
Element fnameElement = doc1.createElement("date");
fnameElement.setTextContent(Array[0][i]);
rootElement.appendChild(fnameElement);
Element snameElement = doc1.createElement("value");
snameElement.setTextContent(Array[1][i]);
rootElement.appendChild(snameElement);
//Element professionElement = doc1.createElement("profession");
//professionElement.setTextContent(tab[i].getProfession());
//personElement.appendChild(professionElement);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource dom = new DOMSource(doc1);
StreamResult result = new StreamResult(new File("C:\\Programy\\employees2.xml"));
transformer.transform(dom, result);
System.out.println("File employee was created.");
} catch (Exception e) {
e.printStackTrace();
}
}
}