java 如何使用java写入现有的XML文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10962267/
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
How to write to an existing XML file using java
提问by Aruna Karunarathna
I've got an xml file looked like this. employees.xml
我有一个像这样的 xml 文件。员工.xml
<Employees>
<Employee>
<FirstName>myFirstName</FirstName>
<LastName>myLastName</LastName>
<Salary>10000</Salary>
<Employee>
</Employees>
Now how do I add new Employee elements to the existing XML file?.. An example code is highly appreciated.
现在如何将新的 Employee 元素添加到现有的 XML 文件中?非常感谢示例代码。
采纳答案by Don Roby
To add to an existing XML file, you generally need to read it in to an internal data structure, add the needed data in the internal form and then write it all out again, overwriting the original file.
要添加到现有的 XML 文件中,您通常需要将其读入内部数据结构,在内部表单中添加所需的数据,然后再次将其全部写入,覆盖原始文件。
The internal structure can be DOM
or one of your own making, and there are multiple ways of both reading it in and writing it out.
内部结构可以是DOM
您自己制作的,也可以是您自己制作的,并且有多种读入和写出的方式。
If the data is reasonably small, DOM is probably easiest, and there is some sample code in the answers to this related question.
如果数据相当小,DOM 可能是最简单的,并且在这个相关问题的答案中有一些示例代码。
If your data is large, DOM will not do. Possible approaches are to use SAX to read and write (though SAX is traditionally only a reading mechanism) as described in an answer to another related question.
如果你的数据很大,DOM 就不行。可能的方法是使用 SAX 进行读取和写入(尽管 SAX 传统上只是一种读取机制),如另一个相关问题的回答中所述。
You might also want to consider JAXBor (maybe even best) StAX.
回答by bmargulies
You can't 'write nodes to an existing XML file.' You can read an existing XML file into memory, add to the data model, and then write a new file. You can rename the old file and write the new file under the old name. But there is no commonly-used Java utility that will modify an XML file in place.
您不能“将节点写入现有的 XML 文件”。您可以将现有的 XML 文件读入内存,添加到数据模型,然后写入新文件。您可以重命名旧文件并以旧名称写入新文件。但是没有常用的 Java 实用程序可以就地修改 XML 文件。
回答by Felix Christy
回答by Francisco Spaeth
I think this link can be useful for you.
我认为此链接对您有用。
Here you have samples how to read / parse, modify (add elements) and save (write to xml file again).
此处提供了如何读取/解析、修改(添加元素)和保存(再次写入 xml 文件)的示例。
The following samples you can find at: http://www.petefreitag.com/item/445.cfm
您可以在以下位置找到以下示例:http: //www.petefreitag.com/item/445.cfm
Read:
读:
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse("/path/to/file.xml");
Modify:
调整:
// attributes
Node earth = doc.getFirstChild();
NamedNodeMap earthAttributes = earth.getAttributes();
Attr galaxy = doc.createAttribute("galaxy");
galaxy.setValue("milky way");
earthAttributes.setNamedItem(galaxy);
// nodes
Node canada = doc.createElement("country");
canada.setTextContent("ca");
earth.appendChild(canada);
Write XML file:
写入 XML 文件:
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
回答by GingerHead
You need to use DOM to write/edit your xml file.
It's very easy:
You just need to create nodes and add attributes to it.
You can also write/edit XSLT files by using DOM.
just search google for DOM java
您需要使用 DOM 来编写/编辑您的 xml 文件。
这很简单:
您只需要创建节点并向其添加属性即可。
您还可以使用 DOM 编写/编辑 XSLT 文件。
只需在谷歌搜索 DOM java