Solved

Saving amended xml file

Posted on 2006-06-18
5
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
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 …

710 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