Solved

Fileoutput stream to write out an xml file

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 36
configure dependency in POM for new database 3 25
arguments to jar 5 26
hibernate example using maven 12 42
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

778 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