Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-如何将XML字符串转换为XML文件?_Java_Xml - Fatal编程技术网

Java-如何将XML字符串转换为XML文件?

Java-如何将XML字符串转换为XML文件?,java,xml,Java,Xml,我想将xml字符串转换为xml文件。我得到一个xml字符串作为输出,到目前为止,我有以下代码: public static void stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

我想将xml字符串转换为xml文件。我得到一个xml字符串作为输出,到目前为止,我有以下代码:

public static void stringToDom(String xmlSource) 
    throws SAXException, ParserConfigurationException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
        //return builder.parse(new InputSource(new StringReader(xmlSource)));
    }
然而,我不太确定我将从这里走向何方。我没有在任何地方创建文件,因此如何将其合并到其中


我正在将xml字符串传递到xmlSource。

只需将xml字符串的内容复制到另一个扩展名为.xml的文件中即可。您可以使用java.io实现同样的功能

如果您只想将字符串的内容放在文件中,那么它实际上是否是XML并不重要。您可以跳过解析(这是一个相对昂贵的操作),只需将
字符串
转储到文件,如下所示:

public static void stringToDom(String xmlSource) 
        throws IOException {
    java.io.FileWriter fw = new java.io.FileWriter("my-file.xml");
    fw.write(xmlSource);
    fw.close();
}
但是,如果您想安全地避免编码问题,正如Joachim所指出的那样,则需要解析。因为从不相信你的意见是一个很好的习惯,所以这可能是更好的方法。它看起来是这样的:

public static void stringToDom(String xmlSource) 
        throws SAXException, ParserConfigurationException, IOException {
    // Parse the given input
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));

    // Write the parsed document to an xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);

    StreamResult result =  new StreamResult(new File("my-file.xml"));
    transformer.transform(source, result);
}

如果您的XML字符串是干净的,可以编写,那么为什么不将其复制到一个文件中,并在末尾添加.XML

使用Java 1.7:

Path pathXMLFile = Paths.get("C:/TEMP/TOTO.XML");
Files.write(pathXMLFile, stringXML.getBytes(), StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
简单快速:)


来源:

这使用平台默认编码,很容易导致格式错误的XML文件。困难的方法(由OP开始)是更安全的方法。如何使用此方法设置xml文件中标记之间的字符串或值?例如,在一个xml文件中,我有and builder.parse(newInputSource(newStringReader(xmlSource));这是无效的。(类型DocumentBuilder中的方法parse(InputStream)不适用于参数(InputSource))我想您指的是XML字符串+1
stringXML.getBytes()
get是平台默认编码中的字节。您应该使用与这里的xml头中使用的相同的方法,以避免以后解析该文件时出现问题。这种情况很少发生。
public static void stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException, TransformerException{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("c:/temp/test.xml"));
    transformer.transform(source, result);
}