Results 1 to 4 of 4
  1. #1
    Solid Snake is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Post 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.:
    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
    It writes it like this:
    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>
    My code :
    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.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Creating a XML file

    there are several things wrong or weird
    -
    Java 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");
    once is enough

    - 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();
    	}
    }

  3. #3
    Solid Snake is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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

  1. Replies: 2
    Last Post: 05-26-2011, 05:38 AM
  2. Creating an XML file with XML DOM
    By b1l0s in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2010, 05:17 AM
  3. creating a file
    By aruna.hcu in forum New To Java
    Replies: 25
    Last Post: 01-06-2010, 06:15 PM
  4. Creating a .jar file
    By Wataru in forum New To Java
    Replies: 3
    Last Post: 07-22-2009, 06:02 AM
  5. Creating jar file
    By Heather in forum Advanced Java
    Replies: 4
    Last Post: 02-11-2009, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •