aspose file tools*
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
Author

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++)
{
buffer.write(t[y]);
System.out.println(t[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

gurpreet
[ 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);

hth

-Rick
Gurpreet Saini
Ranch Hand

Joined: Jun 09, 2002
Posts: 295
Hi,

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

thanks

gurpreet
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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: http://aspose.com/file-tools
 
subject: Converting String to XML file?
 
Similar Threads
Writing to a File
Reading a tab delimited txt file and replacing blanks with zeores.
Which class is suitable ?
How to parse SOAP Xml using java code ?
Doubt about StringTokenizer !!