簡體   English   中英

使用StAX修改一個XML文件

[英]Modifying one XML file using StAX

我有以下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>

我希望在這個XML文件中添加更多字段,如:

1)新員工。

2)新的員工詳細信息,如地址,此處不存在。

3)刪除以前的記錄。

從用戶那里獲取適當的值后,我可以通過我的java代碼相應地修改XML。

假設在執行第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>

如何使用StAX解析器實現此目的? 請幫助我提供一些適當的提示和代碼,以便我如何實現這一目標。 :(

編輯1

這是我希望在添加任何新記錄時調用的函數。

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

每次使用相同的文件路徑調用方法時都不應創建新的編寫器,因為這會覆蓋您的文件。

僅創建(並關閉)writer一次,並將其作為參數傳遞給方法:

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

我會指出一種新的/新穎的方式來做你所描述的,使用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");

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM