简体   繁体   English

使用StAX修改一个XML文件

[英]Modifying one XML file using StAX

I have the following xml file 我有以下xml文件

<?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 : 我希望在这个XML文件中添加更多字段,如:

1) New Employee. 1)新员工。

2) New Employee Details like Address which is not present here. 2)新的员工详细信息,如地址,此处不存在。

3) Delete Previous record. 3)删除以前的记录。

After taking the appropriate values from the user I may modify the XML accordingly through my java code. 从用户那里获取适当的值后,我可以通过我的java代码相应地修改XML。

Suppose after doing point number 1 and 2 my new xml becomes... 假设在执行第1点和第2点后,我的新xml变为......

<?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? 如何使用StAX解析器实现此目的? Please help me by giving some appropriate tips and code as to how I may achieve this. 请帮助我提供一些适当的提示和代码,以便我如何实现这一目标。 :( :(

EDIT 1 编辑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 编辑2我面临的另一个问题是遍历以前的XML ....我如何遍历它以便我的光标在此块后面正确

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

and before the line </Employees> 在行</Employees>

Because I need to call the addNewEmployee() at the proper moment. 因为我需要在适当的时候调用addNewEmployee()

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: 仅创建(并关闭)writer一次,并将其作为参数传递给方法:

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 ... 我会指出一种新的/新颖的方式来做你所描述的,使用VTD-XML ...有很多原因可以解释为什么VTD-XML比为这个问题提供的所有其他解决方案要好得多......这里有一些链接......

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

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

http://sdiwc.net/digital-library/request.php?article=0d947fb50e2f0160a75ac9f6bbf0818a 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");

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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