Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-28-2007, 03:34 PM
Member
 
Join Date: Jun 2007
Posts: 5
FrankyDee is on a distinguished road
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 here but my boss wants it for yesterday , of course, so here I am).

Here is a sample code I used:
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
Code:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE NODE_1> <NODE_1 />
Thanx in advance for helping

Frank
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-27-2008, 04:59 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 226
DonCash will become famous soon enoughDonCash will become famous soon enough
Hey,

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

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.

Code:
output.write("whatever text to be printed out"); output.write(newline);
And at the end of the code make sure you add:

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

I hope this helps!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i save the data Internally(auto save) Rama Koti Reddy AWT / Swing 0 12-10-2007 02:46 PM
save file to database katerinaaa New To Java 0 08-14-2007 01:15 PM
how to save image as jpg? katie New To Java 2 08-06-2007 04:32 AM
Hwlp with "Open", "Save", "Save as..." trill New To Java 1 07-31-2007 08:53 AM
save configuration Peter XML 2 07-04-2007 08:40 AM


All times are GMT +3. The time now is 02:20 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org