要将XML字符串写入文件,您可以使用以下方法:
import xml.etree.ElementTree as ET
xml_string = "<root><body>Hello, world!</body></root>"
root = ET.fromstring(xml_string)
tree = ET.ElementTree(root)
tree.write("output.xml")
这将把XML字符串写入名为output.xml
的文件中。
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
String xmlString = "<root><body>Hello, world!</body></root>";
writeXmlToFile(xmlString, "output.xml");
}
public static void writeXmlToFile(String xmlString, String filePath) {
try {
FileWriter writer = new FileWriter(filePath);
writer.write(xmlString);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这将把XML字符串写入名为output.xml
的文件中。
const fs = require('fs');
const xmlString = "<root><body>Hello, world!</body></root>";
fs.writeFile('output.xml', xmlString, (err) => {
if (err) {
console.error('Error writing file', err);
} else {
console.log('Successfully wrote file');
}
});
这将把XML字符串写入名为output.xml
的文件中。
无论您使用哪种方法,都可以将XML字符串写入文件。
领取专属 10元无门槛券
手把手带您无忧上云