我有以下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()
在适当的时候打电话给。
我会通过使用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");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句