Solved

Saving amended xml file

Posted on 2006-06-18
5
229 Views
Last Modified: 2010-04-17
I have the following program.

How do i save my changes to the document  back?


Thanks.

public static void main(String args[]) throws SAXException, IOException
    {
      

                DocumentBuilder db;
                  try {
                        db = dbf.newDocumentBuilder();
                      Document doc = db.parse(new File("C:\\test.xml"));
                      NodeList nodelist = doc.getElementsByTagName("LEVEL!");
                    
                    
                      NamedNodeMap nodeMap = nodelist.item(0).getAttributes();
                      Node testnode= nodeMap.item(0);
                      testnode.setNodeValue("newValue");
                    
This is where i am having problems. HOw do i save the changes to the xml file Thanks?

                                                      PrintWriter file = new PrintWriter("C:\\test.xml");
                      file.print(doc);
                      file.close();
                      
                  
0
Comment
Question by:wilflife
  • 3
5 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16930605
Hello wilflife,


> public static void main(String args[]) throws SAXException, IOException


hope this helps a bit
bruintje
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16930606
sorry that was not supposed to happen
0
 
LVL 14

Accepted Solution

by:
StillUnAware earned 250 total points
ID: 16930611
Check this sample:

http://javaalmanac.com/egs/javax.xml.transform/WriteDom.html

And Your code would look like:

import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class XMLSaving {
  public static void main(String args[]) throws Exception {
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(new File("C:\\test.xml"));
    NodeList nodelist = doc.getElementsByTagName("LEVEL!");
    NamedNodeMap nodeMap = nodelist.item(0).getAttributes();
    Node testnode= nodeMap.item(0);
    testnode.setNodeValue("newValue");
    Source source = new DOMSource(doc);
    File file = new File("C:\\out.xml");
    Result result = new StreamResult(file);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);
  }
}
0
 
LVL 44

Assisted Solution

by:bruintje
bruintje earned 200 total points
ID: 16930613
for your question i had some thoughts only [assuming Java] along the lines of using the filewriter

http://ws.apache.org/jaxme/release-0.3/manual/ch01s02.html

    FileWriter fw = new FileWriter("Address.xml");
    fw.??(doc);
    fw.close();
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question