Java Course

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 ReadXMLFile {

static String a;
static String b;

public static void main(String argv[]) {
try {
File fXmlFile = new File("C:\\Programy\\data-set.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);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
a = eElement.getElementsByTagName("data").item(0).getTextContent();
b = eElement.getElementsByTagName("wartość").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\\filename.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++)
{
System.out.println(Array[0][i] + " " + Array[1][i]);
}
XMLEncoder e = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("C:\\Programy\\Test.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("data-set");
doc1.appendChild(rootElement);
for(int i=0; i<size;i++)
{
Element personElement = doc1.createElement("record");
//personElement.setAttribute("id");
rootElement.appendChild(personElement);

Element fnameElement = doc1.createElement("date");
fnameElement.setTextContent(Array[0][i]);
personElement.appendChild(fnameElement);

Element snameElement = doc1.createElement("value");
snameElement.setTextContent(Array[1][i]);
personElement.appendChild(snameElement);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource dom = new DOMSource(doc1);
StreamResult result = new StreamResult(new File("C:\\Programy\\employees.xml"));
transformer.transform(dom, result);
System.out.println("File employee was created.");

} catch (Exception e) {
e.printStackTrace();
}
}
}

© 2013-2024 PRV.pl
Strona została stworzona kreatorem stron w serwisie PRV.pl