我有一个XML格式的字符串。我需要将其转换为XML文件。我该怎么做?
答案 0 :(得分:10)
爪哇:
XMLDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader("<root><main>Title</main></root&g t;")));
如果您使用C#,请尝试此
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");
}
答案 1 :(得分:1)
这是一个包含XML的字符串?然后将其写入文件。在Java? FileWriter应该可以正常工作。
答案 2 :(得分:1)
使用XStream库非常简单: 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);
// 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);
答案 3 :(得分:0)
只需将字符串写入扩展名为.xml的文件。这是代码:
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)
{
}
}
}