package com.hcwy.dao;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class Operate {
public void BuildXMLDoc() throws IOException, JDOMException {
// 创建根节点 list;
Element root = new Element("list");
// 根节点添加到文档中;
Document Doc = new Document(root);
// 此处 for 循环可替换成 遍历 数据库表的结果集操作;
for (int i = 0; i < 5; i++) {
// 创建节点 user;
Element elements = new Element("user");
// 给 user 节点添加属性 id;
elements.setAttribute("id", "" + i);
// 给 user 节点添加子节点并赋值;
// new Element("name")中的 "name" 替换成表中相应字段,setText("xuehui")中 "xuehui
// 替换成表中记录值;
Element name = new Element("name");
name.setAttribute("id",""+i);
name.setText("xuehui");
elements.addContent(name);
elements.addContent(new Element("age").setText("28"));
elements.addContent(new Element("sex").setText("Male"));
// 给父节点list添加user子节点;
root.addContent(elements);
}
XMLOutputter XMLOut = new XMLOutputter();
// 输出 user.xml 文件;
XMLOut.output(Doc, new FileOutputStream("user.xml"));
}
public void jiexi() throws Exception { // 注意这里必须要抛出异常,否则会报错
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File("user.xml")); // 导入xml文件,xml文件名取名为test.xml
Element root = doc.getRootElement(); // 获得根节点 这里是list
List tempChildren = root.getChildren(); // 获得LIST的子接点user
for(int i=0;i<tempChildren.size();i++){
Element temp1 = (Element) tempChildren.get(i); // 以得到第一个user结点为例,注意这里必须要强制转化一下
System.out.println("userID是"+temp1.getAttribute("id").getValue());
System.out.println("userName是"+temp1.getChild("name").getText());//取NAME
Element name=temp1.getChild("name");
System.out.println("name的ID是-->"+name.getAttribute("id").getValue());
System.out.println("userAge是"+temp1.getChild("age").getText());//取AGE
System.out.println("userSex是"+temp1.getChild("sex").getText());//取SEX
// Element file = temp1.getChild("name"); 如果name里有属性 那么我门先取得NAME
// System.out.println("file结点id、z的属性值分别是:id='"
// + file.getAttribute("id").getValue() + "' z='"
// + file.getAttribute("z").getValue() + "'");然后把name里的东西取出来
}
}
//修改XML
public void updateXML(){
SAXBuilder builder = new SAXBuilder();
try {
Document doc = builder.build(new File("user.xml"));
Element root = doc.getRootElement(); // 获得根节点 这里是list
List tempChildren = root.getChildren(); // 获得LIST的子接点user
for(int i=0;i<tempChildren.size();i++){
Element temp1 = (Element) tempChildren.get(i); // 以得到第一个user结点为例,注意这里必须要强制转化一下
System.out.println("userID是"+temp1.getAttribute("id").getValue());
System.out.println("userName是"+temp1.getChild("name").getText());//取NAME
Element name=temp1.getChild("name");
System.out.println("name的ID是-->"+name.getAttribute("id").getValue());
System.out.println("userAge是"+temp1.getChild("age").getText());//取AGE
System.out.println("userSex是"+temp1.getChild("sex").getText());//取SEX
name.setText("zzzz");
}
File f=new File("user.xml");
if(f.exists()){
f.delete();
}
XMLOutputter XMLOut = new XMLOutputter();
// 输出 user.xml 文件;
XMLOut.output(doc, new FileOutputStream("user.xml"));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} // 导入xml文件,xml文件名取名为test.xml
}
public static void main(String[] args) {
Operate j2x = new Operate();
/**
*这是生成XML文件
try {
Operate j2x = new Operate();
System.out.println("生成 mxl 文件...");
j2x.BuildXMLDoc();
} catch (Exception e) {
e.printStackTrace();
}
*
*/
/**
*这是解析XML文件
Operate j2x = new Operate();
try {
j2x.jiexi();
} catch (Exception e) {
e.printStackTrace();
}
*
*/
j2x.updateXML();
}
}