Editing a single XML file using StAX

I have the following xml file

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
    <Employee ID="1">
        <Firstname>David</Firstname >
        <Lastname>Berkley</Lastname>
        <Age>30</Age>
        <Salary>25001</Salary>
    </Employee>
    <Employee ID="2">
        <Firstname>Ashton</Firstname>
        <Lastname>Hutt</Lastname>
        <Age>22</Age>
        <Salary>26000</Salary>
    </Employee>

</Employees>

I want to add more fields to this XML file, for example:

1) New employee.

2) New employee data, such as an address that is not here.

3) Delete the previous entry.

After accepting the appropriate values ​​from the user, I can change the XML accordingly through my Java code.

Suppose that after completing points 1 and 2, my new xml will become ...

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
    <Employee ID="1">
            <Firstname>David</Firstname >
            <Lastname>Berkley</Lastname>
            <Age>30</Age>
            <Salary>25001</Salary>
            <Address>10th cross,Park Avenue</Address>
        </Employee>
        <Employee ID="2">
            <Firstname>Ashton</Firstname>
            <Lastname>Hutt</Lastname>
            <Age>22</Age>
            <Salary>26000</Salary>
        </Employee>
    <Employee ID="3">
        <Firstname>Holly</Firstname>
        <Lastname>Becker</Lastname>
        <Age>24</Age>
        <Salary>30000</Salary>
    </Employee>

</Employees>

How can I achieve this. Using the StAX parser? Please help me by pointing out some suitable tips and code regarding how I can achieve this. :(

EDIT 1

This is my function that I want to call when adding a new record.

public void addNewEmployee(XMLStreamWriter writer,String newID, String firstN, String lastN, String age, String salary)
    {


         try 
         {


             writer.writeStartDocument();
             writer.writeStartElement("Employee");
             writer.writeAttribute("ID", newID);



             writer.writeStartElement("Firstname");
             writer.writeCharacters(firstN);
             writer.writeEndElement();

             writer.writeStartElement("Lastname");
             writer.writeCharacters(lastN);
             writer.writeEndElement();

             writer.writeStartElement("Age");
             writer.writeCharacters(age);
             writer.writeEndElement();

             writer.writeStartElement("Salary");
             writer.writeCharacters(salary);
             writer.writeEndElement();



             writer.writeEndElement();
             writer.writeEndDocument();


             writer.flush();
             writer.close();
            // System.out.println("New Record Added");

         } catch (XMLStreamException e) {
             e.printStackTrace();
         }


    }

2 , , - XML.... ,

<Employee ID="2">
            <Firstname>Ashton</Firstname>
            <Lastname>Hutt</Lastname>
            <Age>22</Age>
            <Salary>26000</Salary>
        </Employee>

</Employees>

addNewEmployee() .

+4
2

/ , , VTD-XML... , VTD-XML , , ... ...

http://www.javaworld.com/article/2071745/soa/simplify-xml-processing-with-vtd-xml.html

http://www.devx.com/xml/Article/36379

http://sdiwc.net/digital-library/request.php?article=0d947fb50e2f0160a75ac9f6bbf0818a

import com.ximpleware.*;
import java.io.*;

public class simpleMod {
    public static void main(String s[]) throws VTDException,IOException{
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        XMLModifier xm = new XMLModifier(vn);
        AutoPilot ap = new AutoPilot(vn),ap2=new AutoPilot(vn);
        ap.selectXPath("/employees/employee[@ID='1']/Salary");
        ap2.selectXPath("../../employee[@ID='2'");
        int i=ap.evalXPath();
        if (i==-1)
            return; 
        xm.insertAfterElement("<Address>10th cross, Park Avenue</Address>");
        i=ap2.evalXPath();
        if (i==-1)
            return;
        xm.insertAfterElement(" <Employee ID=\"3\">\n<Firstname>Holly</Firstname>\n<Lastname>Becker</Lastname>\n<Age>24</Age>\n<Salary>30000</Salary>\n</Employee>");
        xm.output("output.xml");

    }
}
+1

, , , .

( ) :

public void addNewEmployee(XMLStreamWriter writer, String newID, String firstN, String lastN, String age, String salary)
+2

Source: https://habr.com/ru/post/1583752/


All Articles