java 将 org.w3c.dom.Document 转换为 File 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37104523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert org.w3c.dom.Document to File file
提问by jer08
I have a xml file as object in Java as org.w3c.dom.Document doc and I want to convert this into File file. How can I convert the type Document to File? thanks
我有一个 xml 文件作为 Java 中的对象,如 org.w3c.dom.Document doc,我想将其转换为文件文件。如何将类型文档转换为文件?谢谢
I want to add metadata elements in an existing xml file (standard dita) with type File. I know a way to add elements to the file, but then I have to convert the file to a org.w3c.dom.Document. I did that with the method loadXML:
我想在类型为 File 的现有 xml 文件(标准 dita)中添加元数据元素。我知道一种向文件添加元素的方法,但是我必须将文件转换为 org.w3c.dom.Document。我用 loadXML 方法做到了这一点:
private Document loadXML(File f) throws Exception{
DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return builder.parse(f);
After that I change the org.w3c.dom.Document, then I want to continue with the flow of the program and I have to convert the Document doc back to a File file.
之后我更改了 org.w3c.dom.Document,然后我想继续程序的流程,我必须将 Document doc 转换回 File 文件。
What is a efficient way to do that? Or what is a better solution to get some elements in a xml File without converting it?
什么是有效的方法来做到这一点?或者什么是在 xml 文件中获取一些元素而不转换它的更好的解决方案?
回答by Mario Cairone
You can use a Transformer class to output the entire XML content to a File, as showed below:
您可以使用 Transformer 类将整个 XML 内容输出到文件中,如下所示:
Document doc =...
// write the content into xml file
DOMSource source = new DOMSource(doc);
FileWriter writer = new FileWriter(new File("/tmp/output.xml"));
StreamResult result = new StreamResult(writer);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
回答by Würgspa?
With JDK 1.8.0 a short way is to use the built-in XMLSerializer (which was introduced with JDK 1.4 as a fork of Apache Xerces)
对于 JDK 1.8.0,一种简单的方法是使用内置的 XMLSerializer(它是在 JDK 1.4 中作为 Apache Xerces 的一个分支引入的)
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
Document doc = //use your method loadXML(File f)
//change Document
java.io.Writer writer = new java.io.FileWriter("MyOutput.xml");
XMLSerializer xml = new XMLSerializer(writer, null);
xml.serialize(doc);
Use an object of type OutputFormat
to configure output, for example like this:
使用类型的对象OutputFormat
来配置输出,例如像这样:
OutputFormat format = new OutputFormat(Method.XML, StandardCharsets.UTF_8.toString(), true);
format.setIndent(4);
format.setLineWidth(80);
format.setPreserveEmptyAttributes(true);
format.setPreserveSpace(true);
XMLSerializer xml = new XMLSerializer(writer, format);
Note that the classes are from com.sun.*
package which is not documented and therefore generally is not seenas the preferred way of doing things. However, with javax.xml.transform.OutputKeys
you cannot specify the amount of indentation or line width for example. So, if this is important then this solution should help.
请注意,这些类来自com.sun.*
没有记录的包,因此通常不被视为首选的做事方式。但是,例如,javax.xml.transform.OutputKeys
您不能指定缩进量或线宽。所以,如果这很重要,那么这个解决方案应该会有所帮助。