如何在 xml 文件中保存解析和更改的 DOM 文档? [英] How to save parsed and changed DOM document in xml file?
本文介绍了如何在 xml 文件中保存解析和更改的 DOM 文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 xml 文件.我需要阅读它,进行一些更改并将新的更改版本写入某个新目的地.
I have xml-file. I need to read it, make some changes and write new changed version to some new destination.
我设法读取、解析和修补此文件(使用 DocumentBuilderFactory
、DocumentBuilder
、Document
等).
I managed to read, parse and patch this file (with DocumentBuilderFactory
, DocumentBuilder
, Document
and so on).
但我找不到如何保存该文件的方法.有没有办法让它成为纯文本视图(如 String
)或其他更好的方法?
But I cannot find a way how to save that file. Is there a way to get it's plain text view (as String
) or any better way?
推荐答案
类似这样的方法:
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(new File("output.xml"));
Source input = new DOMSource(myDocument);
transformer.transform(input, output);
这篇关于如何在 xml 文件中保存解析和更改的 DOM 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文