How to write an existing XML file using java

I have an xml file similar to this one. employees.xml

<Employees> <Employee> <FirstName>myFirstName</FirstName> <LastName>myLastName</LastName> <Salary>10000</Salary> <Employee> </Employees> 

Now, how do I add new Employee elements to an existing XML file? .. The sample code is commendable.

+4
source share
5 answers

To add to an existing XML file, you usually need to read it in the internal data structure, add the necessary data to the internal form, and then write everything again, overwriting the original file.

The internal structure may be the DOM or one of your own decisions, and there are several ways to read and write it.

If the data is small enough, the DOM is probably the easiest, and there is sample code in the answers to this related question .

If your data is large, the DOM will not do. Possible approaches are to use SAX for reading and writing (although SAX has traditionally been the only reading mechanism), as described in the answer to another related question .

You can also consider JAXB or (perhaps even better) StAX .

+2
source

You cannot "write nodes to an existing XML file." You can read the existing XML file in memory, add it 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 generally accepted Java utility that will modify the XML file in place.

+5
source

Use xstream to parse your file as an object or create a list with employees, and then you can directly convert it to xml.

+2
source

I think this link may be useful for you.

Here you have examples of how to read / parse, modify (add elements) and save (write to the xml file again).

The following samples can be found at: http://www.petefreitag.com/item/445.cfm

Reading:

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

XML file entry:

 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); 
+2
source

You need to use the DOM to write / edit the xml file.
It is very simple:
You just need to create nodes and add attributes to it.
You can also write / edit XSLT files using the DOM.
just do a google search for DOM java

+1
source

All Articles