Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to save an XML file from Java

Posted on 2009-03-28
4
Medium Priority
?
5,925 Views
Last Modified: 2013-11-23
Dear fellow Java developers:

I have written some java code that creates an xml document, but I'm having trouble SAVING it on to my local pc as an xml file.  I thought it would be similar to writing a text file, but I am a bit off.  I have listed my code below, and I think I'm almost there, but not quite.  When I run my code, I get the following content within my document.xml file:

[#document: null]

Any help would be greatly appreciated.  Thanks to all who reply.
package com.javatest;
 
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
 
public class CreateXMLDoc {
	
	PrintWriter updates;
	
	public static void main(String[] args) throws Exception {
		String root = "transmission";
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
		Element rootElement = document.createElement(root);
        document.appendChild(rootElement);
		
		
			String element = "message";
			
			String data = "text";
			Element em = document.createElement(element);
			em.appendChild(document.createTextNode(data));
			rootElement.appendChild(em);
		
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result =  new StreamResult(System.out);
        transformer.transform(source, result);
        
        CreateXMLDoc testCode = new CreateXMLDoc();
        testCode.fileWrite(document);
	}
        
        public void fileWrite(Document doc){
        
        try  
        {     
        	updates = new PrintWriter(new BufferedWriter(new FileWriter("c:\\document.xml")), true);
        }  
        catch(Exception e)
        {   e.printStackTrace();
             return;
        }
        updates.println(doc);
	}
 
}

Open in new window

0
Comment
Question by:fsyed
[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
  • 2
4 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 24011944
0
 
LVL 12

Assisted Solution

by:basav_com
basav_com earned 1600 total points
ID: 24013668
0
 
LVL 12

Accepted Solution

by:
basav_com earned 1600 total points
ID: 24013709
I changed the following line in your code:
StreamResult result =  new StreamResult(new File("c:\\document.xml"));

And commented this call:
 //testCode.fileWrite(document);

Check the attached code:

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
 
public class CreateXMLDoc {
        
        PrintWriter updates;
        
        public static void main(String[] args) throws Exception {
                String root = "transmission";
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
                Element rootElement = document.createElement(root);
        document.appendChild(rootElement);
                
                
                        String element = "message";
                        
                        String data = "text";
                        Element em = document.createElement(element);
                        em.appendChild(document.createTextNode(data));
                        rootElement.appendChild(em);
                
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result =  new StreamResult(new File("c:\\document.xml"));
        transformer.transform(source, result);
        
        CreateXMLDoc testCode = new CreateXMLDoc();
        //testCode.fileWrite(document);
        }
        
        public void fileWrite(Document doc){
        
        try  
        {     
                updates = new PrintWriter(new BufferedWriter(new FileWriter("c:\\document.xml")), true);
        }  
        catch(Exception e)
        {   e.printStackTrace();
             return;
        }
        updates.println(doc);
        }
 
}

Open in new window

0
 

Author Closing Comment

by:fsyed
ID: 31563970
Thanks so much for your responses, and your solutions.  I do appreciate it!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

715 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