Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write to an existing XML file using java

Tags:

java

xml

I've got an xml file looked like this. employees.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.

like image 738
Aruna Karunarathna Avatar asked Dec 12 '22 01:12

Aruna Karunarathna


1 Answers

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.

like image 91
bmargulies Avatar answered Dec 20 '22 18:12

bmargulies