Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Saving amended xml file

Posted on 2006-06-18
5
Medium Priority
?
252 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 1000 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 800 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Simple Linear Regression
Loops Section Overview

886 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