Solved

Fileoutput stream to write out an xml file

Posted on 2004-03-30
5
613 Views
Last Modified: 2013-11-23
Dear java experts,
               
 I have an xml as STRING  created
<a>
    <b>test<b>
</a>

How do I write this out to an XML File in a Desired location , I have the path.
thanks,
testsubbu
0
Comment
Question by:Testsubbu
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
Nick_72 earned 500 total points
ID: 10714327
import java.io.*;


try {
  BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\your\\path")));

  bos.write(yourString.getBytes());
  bos.flush();
  bos.close();
}
catch (IOException ioe) {
  System.out.println(ioe.toString());
}
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 10714334
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\your\\path\\yourfile.xml")));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10714367
Try something like:



String s = "<a><b>test<b></a>";

..........


public static void writeXmlFile(String xmlString, String filename) {
        try {
            // Prepare the xml for writing
            Source source = new StreamSource(new StringReader(xmlString));
   
            // Prepare the output file
            File file = new File(filename);
            Result result = new StreamResult(file);
   
            // Write the DOM document to the file
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
        } catch (TransformerException e) {
        }
    }


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10714397
You can't use OutputStreams or even Writers as if the input were to contain certain characters, they would not get handled correctly for the purposes of xml.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10719197
Testsubbu, as i've explained that answer is not correct. It may well work in the case that you've posted, but if you used it to write the following:

String s = "<a><b>1 & 1 == true<b></a>";

The only time you'll find out it doesn't work is when you try to read the resulting file with any xml classes
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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

17 Experts available now in Live!

Get 1:1 Help Now