Solved

Saving amended xml file

Posted on 2006-06-18
5
204 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
Comment Utility
Hello wilflife,


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


hope this helps a bit
bruintje
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
sorry that was not supposed to happen
0
 
LVL 14

Accepted Solution

by:
StillUnAware earned 250 total points
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now