Results 1 to 4 of 4
Thread: Creating a XML file
- 04-06-2012, 06:27 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Creating a XML file
Hello,
I was trying to convert a normal txt file to xml, the elements i need are 5 for example. but for some reason it overwrites all the elements to the last one on the file, e.g.:
It writes it like this:Java Code:spawn = 1157 3497 9495 0 1 0 0 0 Kalphite Guardian spawn = 2881 2907 4445 0 0 38 500 200 Supreme spawn = 2882 2915 4456 0 0 67 500 200 Prime spawn = 2883 2922 4444 0 0 36 500 100 Rex spawn = 3340 1738 5226 0 1 32 300 200 Giant mole
My code :Java Code:<spawns> <SPAWN NPCID="3340"> <POSITION X="1780" Y="5190" Z="0" /> </SPAWN> <SPAWN NPCID="3340"> <POSITION X="1780" Y="5190" Z="0" /> </SPAWN> <SPAWN NPCID="3340"> <POSITION X="1780" Y="5190" Z="0" /> </SPAWN> <SPAWN NPCID="3340"> <POSITION X="1780" Y="5190" Z="0" /> </SPAWN> <SPAWN NPCID="3340"> <POSITION X="1780" Y="5190" Z="0" /> </SPAWN> <SPAWN NPCID="3340"> <POSITION X="1780" Y="5190" Z="0" /> </SPAWN> </spawns>
Java Code:import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; public class Convert { private void write(int npcIdS, int npcXS, int npcYS, int npcZS) { try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("spawns"); doc.appendChild(rootElement); for (int i = 0;i < 5;i++) { // staff elements Element spawn = doc.createElement("SPAWN"); rootElement.appendChild(spawn); // NPC ID Attr ID = doc.createAttribute("NPCID"); String npcId = Integer.toString(npcIdS); ID.setValue(npcId); spawn.setAttributeNode(ID); // Position Element position = doc.createElement("POSITION"); spawn.appendChild(position); // X coordinate Attr xCoordinate = doc.createAttribute("X"); String npcX = Integer.toString(npcXS); xCoordinate.setValue(npcX); position.setAttributeNode(xCoordinate); // Y coordinate Attr yCoordinate = doc.createAttribute("Y"); String npcY = Integer.toString(npcYS); yCoordinate.setValue(npcY); position.setAttributeNode(yCoordinate); // Z coordinate (height) Attr zCoordinate = doc.createAttribute("Z"); String npcZ = Integer.toString(npcZS); zCoordinate.setValue(npcZ); position.setAttributeNode(zCoordinate); } // Writes the content into the XML file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("C:\\npc-spawns.xml")); transformer.transform(source, result); System.out.println("NPC: " + npcIdS); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (TransformerException tfe) { tfe.printStackTrace(); } } private void read(String fileName) { String line = ""; String token = ""; String token2 = ""; String token2_2 = ""; String[] token3 = new String[10]; boolean EndOfFile = false; BufferedReader characterfile = null; try { characterfile = new BufferedReader(new FileReader("C:\\" + fileName)); } catch(FileNotFoundException fileex) { System.out.println(fileName+": file not found."); return; } try { line = characterfile.readLine(); } catch(IOException ioexception) { System.out.println(fileName+": error loading file."); return; } while(EndOfFile == false && line != null) { line = line.trim(); int spot = line.indexOf("="); if (spot > -1) { token = line.substring(0, spot); token = token.trim(); token2 = line.substring(spot + 1); token2 = token2.trim(); token2_2 = token2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t"); token3 = token2_2.split("\t"); if (token.equals("spawn")) write(Integer.parseInt(token3[0]), Integer.parseInt(token3[1]), Integer.parseInt(token3[2]), Integer.parseInt(token3[3])); } else { if (line.equals("[ENDOFSPAWNLIST]")) { try { characterfile.close(); } catch(IOException ioexception) { } } } try { line = characterfile.readLine(); } catch(IOException ioexception1) { EndOfFile = true; } } try { characterfile.close(); } catch(IOException ioexception) { } return; } Convert() { read("npc-spawns.cfg"); } public static void main(String[] args) { new Convert(); } }Last edited by Solid Snake; 04-06-2012 at 06:30 PM.
- 04-06-2012, 06:59 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Creating a XML file
there are several things wrong or weird
-once is enoughJava Code:token2_2 = token2_2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t");
- why do you iterate 5 times in your write method? That makes no sense, the write method will be invoked five times with your while loop / bufferedreader!
- why you create each time a new factory+docbuilder+document+rootelement!
- write the content only once (at the end, not in the write method!)
-->
Java Code:import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; public class Convert { private DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); private DocumentBuilder docBuilder; private Document doc; private Element rootElement; private void write(int npcIdS, int npcXS, int npcYS, int npcZS) { // staff elements Element spawn = doc.createElement("SPAWN"); rootElement.appendChild(spawn); // NPC ID Attr ID = doc.createAttribute("NPCID"); String npcId = Integer.toString(npcIdS); ID.setValue(npcId); spawn.setAttributeNode(ID); // Position Element position = doc.createElement("POSITION"); spawn.appendChild(position); // X coordinate Attr xCoordinate = doc.createAttribute("X"); String npcX = Integer.toString(npcXS); xCoordinate.setValue(npcX); position.setAttributeNode(xCoordinate); // Y coordinate Attr yCoordinate = doc.createAttribute("Y"); String npcY = Integer.toString(npcYS); yCoordinate.setValue(npcY); position.setAttributeNode(yCoordinate); // Z coordinate (height) Attr zCoordinate = doc.createAttribute("Z"); String npcZ = Integer.toString(npcZS); zCoordinate.setValue(npcZ); position.setAttributeNode(zCoordinate); } private void read(String fileName) { String line = ""; String token = ""; String token2 = ""; String token2_2 = ""; String[] token3 = new String[10]; boolean EndOfFile = false; BufferedReader characterfile = null; try { characterfile = new BufferedReader(new FileReader(fileName)); } catch (FileNotFoundException fileex) { System.out.println(fileName + ": file not found."); return; } try { line = characterfile.readLine(); } catch (IOException ioexception) { System.out.println(fileName + ": error loading file."); return; } while (EndOfFile == false && line != null) { line = line.trim(); int spot = line.indexOf("="); if (spot > -1) { token = line.substring(0, spot); token = token.trim(); token2 = line.substring(spot + 1); token2 = token2.trim(); token2_2 = token2.replaceAll("\t\t", "\t"); token3 = token2_2.split("\t"); if (token.equals("spawn")) write(Integer.parseInt(token3[0]), Integer.parseInt(token3[1]), Integer.parseInt(token3[2]), Integer.parseInt(token3[3])); } else { if (line.equals("[ENDOFSPAWNLIST]")) { try { characterfile.close(); } catch (IOException ioexception) { } } } try { line = characterfile.readLine(); } catch (IOException ioexception1) { EndOfFile = true; } } // Writes the content into the XML file try { TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("npc-spawns.xml")); transformer.transform(source, result); characterfile.close(); } catch (IOException ioexception) { } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return; } Convert() { try { docBuilder = docFactory.newDocumentBuilder(); doc = docBuilder.newDocument(); rootElement = doc.createElement("spawns"); doc.appendChild(rootElement); } catch (ParserConfigurationException e) { e.printStackTrace(); } read("npc-spawns.cfg"); } public static void main(String[] args) { new Convert(); } }
- 04-06-2012, 07:27 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Re: Creating a XML file
Super thanks, it works now.
Also the read code is copied directly from the source. I only made the write code.
I should notice all that next time.
Thanks again :)
One last question..
Is there any way to make it put lines to make it readable ?
current:
I think it's called "sorting" ?Last edited by Solid Snake; 04-06-2012 at 07:37 PM.
- 04-06-2012, 07:43 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Creating a XML file
You could use the transformer method:
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
Or for better output you could use com.sun.org.apache.xml.internal.serialize.XMLSeria lizer:
Java Code:OutputFormat format = new OutputFormat(doc); format.setIndenting(true); XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File("C:\\npc-spawns.xml")), format); serializer.serialize(doc);
Similar Threads
-
Not able to delete the file from the temp location after creating a zip file.
By renu in forum New To JavaReplies: 2Last Post: 05-26-2011, 05:38 AM -
Creating an XML file with XML DOM
By b1l0s in forum Advanced JavaReplies: 0Last Post: 02-12-2010, 05:17 AM -
creating a file
By aruna.hcu in forum New To JavaReplies: 25Last Post: 01-06-2010, 06:15 PM -
Creating a .jar file
By Wataru in forum New To JavaReplies: 3Last Post: 07-22-2009, 06:02 AM -
Creating jar file
By Heather in forum Advanced JavaReplies: 4Last Post: 02-11-2009, 09:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks