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

时间:2022-05-07 23:03:46

I am wanting to convert a xml string to a xml file. I am getting a xml string as an out put and I have the following code so far:

我想将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)));
    }

However Im not too sure where I go from here. I am not creating the file anywhere, so how do I incorporate that into it?

但是我不太确定我从哪里开始。我不是在任何地方创建文件,所以如何将其合并到其中?

I am passing my xml string into xmlSource.

我将我的xml字符串传递给xmlSource。

4 个解决方案

#1


22  

If you just want to put the content of a String in a file, it doesn't really matter whether it is actually XML or not. You can skip the parsing (which is a relatively expensive operation) and just dump the String to file, like so:

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

public static void stringToDom(String xmlSource) 
        throws IOException {
    java.io.FileWriter fw = new java.io.FileWriter("my-file.xml");
    fw.write(xmlSource);
    fw.close();
}

If you want to be on the safe side and circumvent encoding issues, as pointed by Joachim, you would need parsing however. Since its good practice to never trust your inputs, this might be the preferable way. It would look like this:

如果你想要安全并避免编码问题,正如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);
}

#2


1  

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);
}  

Source : http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT4.html

资料来源:http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT4.html

#3


0  

If your XML string is clean and ready to be written, why don't you copy it into a file with .xml at the end ?

如果您的XML字符串是干净的并且可以写入,那么为什么不将它复制到最后带有.xml的文件中?

With Java 1.7 :

使用Java 1.7:

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

Easy and quick :)

简单快捷:)

#4


0  

Just copy the contents of XML string to another file with extension .xml . You can use java.io for the same .

只需将XML字符串的内容复制到扩展名为.xml的另一个文件即可。你可以使用java.io。

#1


22  

If you just want to put the content of a String in a file, it doesn't really matter whether it is actually XML or not. You can skip the parsing (which is a relatively expensive operation) and just dump the String to file, like so:

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

public static void stringToDom(String xmlSource) 
        throws IOException {
    java.io.FileWriter fw = new java.io.FileWriter("my-file.xml");
    fw.write(xmlSource);
    fw.close();
}

If you want to be on the safe side and circumvent encoding issues, as pointed by Joachim, you would need parsing however. Since its good practice to never trust your inputs, this might be the preferable way. It would look like this:

如果你想要安全并避免编码问题,正如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);
}

#2


1  

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);
}  

Source : http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT4.html

资料来源:http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT4.html

#3


0  

If your XML string is clean and ready to be written, why don't you copy it into a file with .xml at the end ?

如果您的XML字符串是干净的并且可以写入,那么为什么不将它复制到最后带有.xml的文件中?

With Java 1.7 :

使用Java 1.7:

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

Easy and quick :)

简单快捷:)

#4


0  

Just copy the contents of XML string to another file with extension .xml . You can use java.io for the same .

只需将XML字符串的内容复制到扩展名为.xml的另一个文件即可。你可以使用java.io。