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
2009-12-17 05:13:30
+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
2009-12-17 05:43:56
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
2009-12-17 06:39:10
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
2009-12-17 17:03:05
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
2009-12-17 06:20:57