File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Converting String to XML file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Converting String to XML file?" Watch "Converting String to XML file?" New topic

Converting String to XML file?

Gurpreet Saini
Ranch Hand

Joined: Jun 09, 2002
Posts: 295
hello guys,

I have a String variable it contains xml data but as a String now I want to convert it into File ?. How is that possible ?. Please guide me .
I am using the following code:

String xmlfile = XMLUtil.getXMLFileName(data).trim();

StringTokenizer token = new StringTokenizer(data, ">");
String t [] = new String [token.countTokens()];

File file = new File("/processed/" + xmlfile + ".xml");

for (int i =0 ; token.hasMoreTokens(); i++)
t [i] = token.nextToken().concat(">").trim();

FileWriter writer = new FileWriter(file);
BufferedWriter buffer = new BufferedWriter(writer);

for (int y = 0; y < t.length; y++)

The file is created in process folder but its empty can you guide me please .

I am using Windows 2003 Server service pack 2

thanks in advance

[ March 19, 2008: Message edited by: Gurpreet Saini ]
Ricky Murphy
Ranch Hand

Joined: Oct 16, 2007
Posts: 31
this is how I do it:

DocumentBuilder db = dbf.newDocumentBuilder();
StringReader strReader=new StringReader(xmlString);
InputSource is=new InputSource(strReader);
Document doc=db.parse(is);


Gurpreet Saini
Ranch Hand

Joined: Jun 09, 2002
Posts: 295

Thank you very much for the response but I want to create the File object. How shall I do that from Document object?.


Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

If you have a String that contains an XML document, just use plain old Java I/O to write it to a file.
Gurpreet Saini
Ranch Hand

Joined: Jun 09, 2002
Posts: 295
hi paul

Thank you
I agree. Here's the link:
subject: Converting String to XML file?
It's not a secret anymore!