如何在xml文件中保存解析和更改的DOM文档? [英] How to save parsed and changed DOM document in xml file?
本文介绍了如何在xml文件中保存解析和更改的DOM文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设法读取,解析和修补这个文件(使用 DocumentBuilderFactory
, DocumentBuilder
,文档
等等)。
但是我找不到如何保存该文件的方法。有没有办法得到它的纯文本视图(如 String
)或任何更好的方法?
解决方案
这样做有用:
变压器变压器= TransformerFactory.newInstance()。newTransformer();
Result output = new StreamResult(new File(output.xml));
源输入=新DOMSource(myDocument);
transformer.transform(input,output);
I have xml-file. I need to read it, make some changes and write new changed version to some new destination.
I managed to read, parse and patch this file (with DocumentBuilderFactory
, DocumentBuilder
, Document
and so on).
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?
解决方案
Something like this works:
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屋!
查看全文