Results 1 to 2 of 2
Thread: How can I save my XML
- 09-28-2007, 02:34 PM #1
Member
- Join Date
- Jun 2007
- Posts
- 5
- Rep Power
- 0
How can I save my XML
Hi there,
I'm a bit of a newbie in Java so please, bare with me.
I have a piece of code that opens an existing XML, adds a node in the 1st element. So far so good. The thing is I didn't find out how to save my XML (I'm pretty sure I'm being dumb :rolleyes: here but my boss wants it for yesterday :mad: , of course, so here I am).
Here is a sample code I used:
And the XMLJava Code:import java.io.IOException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import com.sun.org.apache.xerces.internal.parsers.DOMParser; public class Test { private static String sXMLFilename = "C:\\temp\\test.XML"; /** * @param args */ public static void main(String[] args) { // Parse the file DOMParser parser = new DOMParser(); System.out.println("Parsing: " + sXMLFilename); try { parser.parse(sXMLFilename); } catch (SAXException e) { e.printStackTrace(); return; } catch (IOException e) { e.printStackTrace(); return; } // Get the doc System.out.println("\tOK\nGetting the Document object"); Document m_doc = parser.getDocument(); // Get the node System.out.println("\tOK\nGetting NODE_1"); NodeList nodes = m_doc.getElementsByTagName("NODE_1"); // Store it in case we need to create the DRAWbridge node // We assume it exists so no testing here (will be though) Element element = (Element) nodes.item(0); // Check the sub node exists nodes = m_doc.getElementsByTagName("NODE_2"); if (nodes == null || nodes.getLength() == 0) { System.out.println("\tCreating new element"); Element newEl = element.getOwnerDocument().createElement("NODE_2"); System.out.println("\tOK\nAdding to NODE_1"); element.appendChild(newEl); // Now that my new node is insterted, how do I save the XML files ? System.out.println("\tOK\nSaving XML"); } } }
Thanx in advance for helpingJava Code:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE NODE_1> <NODE_1 />
Frank
- 03-27-2008, 03:59 PM #2
Hey,
Try this to write the file back out as .xml
Firstly add this bit of code just above the DOM parse.
To write to the file you need to add the code below. I haven't had a chance to play with your example yet so you will need to experiment with the placement of this code to make it work correctly.Java Code:Writer output = null; output = new BufferedWriter(new FileWriter("docname.xml")); String newline = System.getProperty("line.separator");
And at the end of the code make sure you add:Java Code:output.write("whatever text to be printed out"); output.write(newline);
Otherwise nothing will be written to the file.Java Code:output.close();
I hope this helps!
Similar Threads
-
Hwlp with "Open", "Save", "Save as..."
By trill in forum New To JavaReplies: 3Last Post: 11-02-2010, 09:26 AM -
How can i save the data Internally(auto save)
By Rama Koti Reddy in forum AWT / SwingReplies: 2Last Post: 11-01-2010, 08:31 PM -
how to save image as jpg?
By katie in forum New To JavaReplies: 4Last Post: 04-15-2010, 10:07 AM -
save file to database
By katerinaaa in forum New To JavaReplies: 0Last Post: 08-14-2007, 12:15 PM -
save configuration
By Peter in forum XMLReplies: 2Last Post: 07-04-2007, 07:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks