tags:

views:

182

answers:

4

Hi All, I have String in xml format i need to convert that to a xml file,how would i do this?

+8  A: 

Hai if you use c#,

Try this

protected void Button1_Click(object sender, EventArgs e)
{
    XmlDocument XDoc = new XmlDocument();
    XDoc.LoadXml("<Root><body>hello</body></Root>");

    XDoc.Save(@"D:\Temp\MyXMl.xml");
}

Java:

XMLDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader("<root><main>Title</main></root&g t;")));
Pandiya Chendur
+1  A: 

It's a string which contains XML? Then just write it to a file. In Java? A FileWriter should work just fine.

Paul Clapham
It's not that easy. The character encoding must be determined from the document header if present, otherwise UTF-8 must be chosen. Simply using a FileWriter and the platform's default encoding is in most situations wrong.
jarnbjo
That's true, I was assuming that it wouldn't have a prolog. If it doesn't, then you would have to use a writer using UTF-8 or UTF-16 as its encoding.
Paul Clapham
A: 

Just write the string to a file with .xml extension.Here is the code:

import java.io.*;
class writeXML {
public static void main(String args[])
{
try{
String s="<xmltag atr=value>tag data</xmltag>";
FileWriter fr= new FileWriter(new File("a.txt"));
Writer br= new BufferedWriter(fr); 
br.write(s);
br.close();
}
catch(Exception e) {}
}
}
CJ
A: 

how can we do the same with flex3.

madhu