感觉还是用java操作起来比较方便
用jdom做对xml文件的读取操作
Student.java
读取
添加内容到XML文件
将内容按照指定编码输出
xml文件内容
测试输出内容和添加内容
结果:
用jdom做对xml文件的读取操作
Student.java
public class Student {
private String num;
private String name;
private String age;
private String sex;
setter...
getter...
读取
public static Document doc;
File xmlFile = new File("E:\\xmltest.xml");
/**
* 读取一个XML文件,遍历其节点,将每一个节点的属性保存到实体中
* @author SavageGarden
* @param file
* @return
*/
public static ArrayList getEntityListByXmlFile(File file) {
ArrayList students = new ArrayList();
SAXBuilder sb = new SAXBuilder();
try {
doc = sb.build(file);
Element root = doc.getRootElement();
List list = root.getChildren("Student");
for(int i = 0; i < list.size(); i++){
Element xmlStudent = (Element)list.get(i);
Student javaStudent= new Student();
javaStudent.setNum(xmlStudent.getAttributeValue("num"));
javaStudent.setName(xmlStudent.getAttributeValue("name"));
javaStudent.setAge(xmlStudent.getAttributeValue("age"));
javaStudent.setSex(xmlStudent.getAttributeValue("sex"));
students.add(javaStudent);
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return students;
}
/**
* 输出实体的属性值
* @author SavageGarden
* @param students
*/
public static void printEntity(ArrayList students) {
for (int i = 0; i < students.size(); i++) {
Student student = (Student)students.get(i);
System.out.println("编号:"+student.getNum()+"--姓名:"+student.getName()+"--年龄:"+student.getAge()+"--性别:"+student.getSex());
}
}
添加内容到XML文件
/**
* 将实体属性添加到XML中
* @author SavageGarden
* @param student
* @param document
* @return
*/
public static Document addEntityToXML(Student student,Document document) {
Element root = document.getRootElement();
Element xmlStudent = new Element("Student");
if(xmlStudent != null) {
xmlStudent.setAttribute("num", student.getNum());
xmlStudent.setAttribute("name", student.getName());
xmlStudent.setAttribute("age", student.getAge());
xmlStudent.setAttribute("sex", student.getSex());
root.addContent(xmlStudent);
}
return document;
}
将内容按照指定编码输出
/**
* 将生成的XML根据指定的编码写到指定文件
* @author SavageGarden
* @param document
* @param file
*/
public static void outXMLToFile(Document document,File file,String charsetName){
XMLOutputter xmlOut = new XMLOutputter("", true, charsetName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
xmlOut.output(document, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将生成的XML根据指定的编码转为字符串
* @author SavageGarden
* @param document
* @param charsetName
* @return
*/
public static String outXMLToString(Document document,String charsetName){
XMLOutputter xmlOut = new XMLOutputter("", true, charsetName);
return xmlOut.outputString(document);
}
xml文件内容
<?xml version="1.0" encoding="GB2312"?>
<Students>
<Student num="1" name="张三" age="22" sex="男"/>
<Student num="2" name="李四" age="23" sex="男"/>
<Student num="3" name="王五" age="24" sex="男"/>
</Students>
测试输出内容和添加内容
File xmlFile = new File("E:\\xmlForYaWu.xml");
printEntity(getEntityListByXmlFile(xmlFile));
Student student = new Student();
student.setNum("4");
student.setName("李六");
student.setAge("34");
student.setSex("女");
outXMLToFile(addEntityToXML(student,doc),xmlFile,"GB2312");
printEntity(getEntityListByXmlFile(xmlFile));
结果:
编号:1--姓名:张三--年龄:22--性别:男
编号:2--姓名:李四--年龄:23--性别:男
编号:3--姓名:王五--年龄:24--性别:男
编号:1--姓名:张三--年龄:22--性别:男
编号:2--姓名:李四--年龄:23--性别:男
编号:3--姓名:王五--年龄:24--性别:男
编号:4--姓名:李六--年龄:34--性别:女