Solved

Saving amended xml file

Posted on 2006-06-18
5
235 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
How to Convert a Quote to an Order in SalesForce 2 116
Generate Unique ID in VB.NET 21 103
Vector, list Questions R 6 46
AvlTree-Node Data type 4 24
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn different types of Android Layout and some basics of an Android App.
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…

749 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