Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting String to XML file?

 
Gurpreet Saini
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi paul

Thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic