Results 1 to 2 of 2
  1. #1
    FrankyDee is offline Member
    Join Date
    Jun 2007
    Posts
    5
    Rep Power
    0

    Default 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:
    Java 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");
    	}
        }
    }
    And the XML
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE NODE_1>
    <NODE_1 />
    Thanx in advance for helping

    Frank

  2. #2
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Hey,

    Try this to write the file back out as .xml
    Firstly add this bit of code just above the DOM parse.

    Java Code:
    Writer output = null;
    output = new BufferedWriter(new FileWriter("docname.xml"));
    String newline = System.getProperty("line.separator");
    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:
    output.write("whatever text to be printed out");
    output.write(newline);
    And at the end of the code make sure you add:

    Java Code:
    output.close();
    Otherwise nothing will be written to the file.

    I hope this helps!

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 10:26 AM
  2. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 09:31 PM
  3. how to save image as jpg?
    By katie in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 11:07 AM
  4. save file to database
    By katerinaaa in forum New To Java
    Replies: 0
    Last Post: 08-14-2007, 01:15 PM
  5. save configuration
    By Peter in forum XML
    Replies: 2
    Last Post: 07-04-2007, 08:40 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
  •