Vous êtes sur la page 1sur 6

package rec;

import java.io.*; import java.util.StringTokenizer;

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 { static int numItems = 200000; static String[] myarray = new String[numItems];

static String[] myarray2 = new String[numItems];

static int cnt=0; static Element staff; public static void main(String[] args) throws Exception {

FileInputStream fstream = new FileInputStream("c:/Order.txt"); // Get the object of DataInputStream

DataInputStream ihn = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(ihn)); //Read File Line By Line String strLine = br.readLine(); // Print the content on the console System.out.println (strLine);

StringTokenizer tokens = new StringTokenizer(strLine,"_"); int i = 0; while(tokens.hasMoreTokens()) { myarray[i] = tokens.nextToken(); i++; } int t=0; for(int x = 0; x <=i-1; x++){

String str=myarray[x]; StringTokenizer tokens2 = new StringTokenizer(str,"="); while(tokens2.hasMoreTokens()) { myarray2[t++] = tokens2.nextToken(); } }

System.out.println(myarray2[0]); System.out.println(myarray2[1]); System.out.println(myarray2[2]); System.out.println(myarray2[3]); System.out.println(myarray2[4]); System.out.println(myarray2[5]); System.out.println(myarray2[6]); System.out.println(myarray2[7]); System.out.println(myarray2[8]); System.out.println(myarray2[9]); System.out.println(myarray2[10]); System.out.println(myarray2[11]); System.out.println(myarray2[12]); System.out.println(myarray2[13]); System.out.println(myarray2[14]); System.out.println(myarray2[15]); System.out.println(myarray2[16]); System.out.println(myarray2[17]); System.out.println(myarray2[18]); System.out.println(myarray2[19]); System.out.println(myarray2[20]); System.out.println(myarray2[21]); System.out.println(myarray2[22]); System.out.println(myarray2[23]);

System.out.println(myarray2[24]); System.out.println(myarray2[25]); System.out.println(myarray2[26]); System.out.println(myarray2[27]); System.out.println(myarray2[28]); System.out.println(myarray2[29]); System.out.println(myarray2[30]); System.out.println(myarray2[31]); System.out.println(myarray2[32]); System.out.println(myarray2[33]); System.out.println(myarray2[34]);

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("Order"); 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");

for (int ii = 0; ii <= 2*(i-1) ; ii++){ if(myarray2[ii].equals("8")) { cnt++; staff = doc.createElement("Order"); rootElement.appendChild(staff); Attr attr = doc.createAttribute("id"); attr.setValue(""+cnt); staff.setAttributeNode(attr); } System.out.print(myarray2[ii]+" "); Element firstname = doc.createElement(("_"+myarray2[ii])); firstname.appendChild(doc.createTextNode(myarray2[++ii])); staff.appendChild(firstname);

} TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("C:\\Order.xml"));

// Output to console for testing // StreamResult result = new StreamResult(System.out);

transformer.transform(source, result);

System.out.println("File saved!"); } }