Solved

Fileoutput stream to write out an xml file

Posted on 2004-03-30
5
617 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

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

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 38
String array comparison 4 46
Java Restore security prompts not working 10 140
Find out all repos that a user is most active on Github 1 33
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 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