解析xml文件

1.导入dom4j.jar;
2.主代码:
<1>查询xml文件的数据:
(1)public void parseXml() throws DocumentException {
SAXReader reader = new SAXReader();
Document doc = reader.read("src/person.xml");
Element root = doc.getRootElement();
Iterator<Object> it_person = root.elementIterator();
while (it_person.hasNext()) {
Element person = (Element) it_person.next();
Iterator<Object> it_child = person.elementIterator();
int i = 0;
String[] str = new String[3];
while (it_child.hasNext()) {
Element p_info = (Element) it_child.next();
str[i] = p_info.getText();
i++;
}
System.out.println(str[0]+ "的性别:" + str[1] +","+ str[0]+ "的年
龄:" + str[2]);
}
}
(2)List persons = root.elements();//root同于上面的root
for (Object person : persons) {
Element p = (Element) person;
Iterator<Element> it_child = p.elementIterator();
int i = 0;
String[] str = new String[3];
while (it_child.hasNext()) {
Element p_info = (Element) it_child.next();
str[i] = p_info.getText();
i++;
}
System.out.println(str[0] + "的性别:" + str[1] + "," + str[0] + "的
年龄:"+ str[2]);
}
}
(3)public void parseXml2() throws DocumentException {
SAXReader reader = new SAXReader();
Document doc = reader.read("src/person.xml");
doc.accept(new VisitorSupport() {
public void visit(Element e) {
System.out.println(e.getText());
}
});
}
<4>创建xml文件
public void createDoc() throws IOException {
Document doc=DocumentHelper.createDocument();
doc.setXMLEncoding("gbk");
Element lib=doc.addElement("libary");//创建根节点libary
Element book=lib.addElement("book");
Element book_name=book.addElement("name");
book_name.setText("大话西游");
Element book_author=book.addElement("auther");
book_author.setText("周星驰");
XMLWriter xw = new XMLWriter(new FileWriter("d:/lib.xml"));
xw.write(doc);
xw.close();

OutputFormat of=OutputFormat.createPrettyPrint();
of.setEncoding("gbk");
XMLWriter xw = new XMLWriter(new FileWriter("d:/lib.xml"),of);
xw.write(doc);
xw.close();
}
<5>向xml文件添加数据:
public void addElement(Element e) throws Exception {
SAXReader reader = new SAXReader();
Document doc = reader.read("src/person.xml");
Element root = doc.getRootElement();
Element person = root.addElement("person");
Element pname = person.addElement("name");
pname.setText("aonier");
Element psex = person.addElement("sex");
psex.setText("male");
Element page = person.addElement("age");
page.setText("37");

OutputFormat of = OutputFormat.createPrettyPrint();
of.setEncoding("gbk");
XMLWriter xw = new XMLWriter(new FileWriter("src/person.xml"), of);
xw.write(doc);
xw.close();
}
<6>修改xml文件
(1)public void updateElement() throws DocumentException {
SAXReader reader = new SAXReader();
final Document doc = reader.read("src/person.xml");
doc.accept(new VisitorSupport() {
public void visit(Element e) {
if (e.getName().equals("age")&&null!=e.getName()
&&!("".equals(e.getName()))) {
int i = Integer.parseInt(e.getTextTrim())* 2;
e.setText(String.valueOf(i));
}
try {
OutputFormat of = OutputFormat.createPrettyPrint();
of.setEncoding("gbk");
XMLWriter xw = new XMLWriter(new FileWriter(
"src/person.xml"), of);
xw.write(doc);
xw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
(2)导入jaxen-1.1.1.jar;
(3)public void updateElement2() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document doc = reader.read("src/person.xml");
List<Object> ages = doc.selectNodes("/class/person/age");
for (Object age : ages) {
Element eage = (Element) age;
int age2 = Integer.parseInt(eage.getText());
if (age2 > 50) {
eage.setText(age2 * 2 + "");
}
}
OutputFormat of = OutputFormat.createPrettyPrint();
of.setEncoding("gbk");
XMLWriter xw = new XMLWriter(new FileWriter("src/person.xml"), of);
xw.write(doc);
xw.close();
}
(4)添加属性:
public void addAttribute() throws Exception{
SAXReader reader = new SAXReader();
Document doc = reader.read("src/person.xml");
List ages  = doc.selectNodes("/class/person/age");
for(Object a : ages){
Element age = (Element)a;
Date d = new Date();
d.setYear(d.getYear()- Integer.parseInt(age.getTextTrim()));
age.addAttribute("birth", d.toLocaleString());
}
OutputFormat of = OutputFormat.createPrettyPrint();
of.setEncoding("gbk");
XMLWriter xw = new XMLWriter(new FileWriter("src/person.xml"), of);
xw.write(doc);
xw.close();
}

你可能感兴趣的:(xml)