简体   繁体   中英

Modifying one 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 wish to add more fields in this XML file like :

1) New Employee.

2) New Employee Details like Address which is not present here.

3) Delete Previous record.

After taking the appropriate values from the user I may modify the XML accordingly through my java code.

Suppose after doing point number 1 and 2 my new xml becomes...

<?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 giving some appropriate tips and code as to how I may achieve this. :(

EDIT 1

This is my function which I wish to call while adding any 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();
         }


    }

EDIT 2 Another issue I am facing is while traversing the previous XML.... How can i traverse it so that my cursor goes right after this block

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

and before the line </Employees>

Because I need to call the addNewEmployee() at the proper moment.

You shouldn't create a new writer every time you call the method using the same file path as this will overwrite your file.

Create (and close) the writer only once and pass it as an argument to the method instead:

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

I would point you to a new/novel way of doing what you described, by using VTD-XML ... there are numerous reasons why VTD-XML is far better than all other solutions provided for this question... here are a few links ...

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM