I have a string in XML format. I need to convert开发者_StackOverflow that to an XML file. How would I do this?
Java:
XMLDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader("<root><main>Title</main></root&g t;")));
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");
}
It's a string which contains XML? Then just write it to a file. In Java? A FileWriter should work just fine.
Use XStream library it is quite simple: http://x-stream.github.io/tutorial.html
// object -> XML -> File
XStream xstream = new XStream(driver);
String data = xstream.toXML(metaData);
// XML -> object
XStream xstream = new XStream(new JettisonMappedXmlDriver());
YourClass obj = (UourClass)xstream.fromXML(jSON);
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)
{
}
}
}
精彩评论