?
Solved

Fileoutput stream to write out an xml file

Posted on 2004-03-30
5
Medium Priority
?
620 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
[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
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
Nick_72 earned 2000 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

752 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