xml文件Extensive Makeup Language是一种常见的配置文件,我们可以通过dom4j对其数据进行度和写 //在导入dom4j的jar包之后/*读取xml文件数据的核心代码为:SAXReader reader = new SAXReader();//创建一个sa
//在导入dom4j的jar包之后 /*读取xml文件数据的核心代码为: SAXReader reader = new SAXReader();//创建一个sax解析器 Document doc = reader.read(new File("文件路径")); Element root = doc.getRootElement();//获取到该文件的唯一根节点 List elements = root.getElements();//获取根节点下所有子节点 //可以写一个遍历获取所有子节点的方法,然后递归 */ /*创建一个xml文件,往里面写数据的核心代码为 Element root = DocumentHelper.createElement("root");//通过DocumentHelper创建一个节点 Document doc = DocumentHelper.createDocument(root);//以root为根节点,在内存创建一个文档 Element son = DocumentHelper.createElement("son"); root.add(son);//把son添加到root下面,作为子节点 FileOutputStream out = new FileOutputStream("src/writeDemo.xml");//创建文件字符输出流 OutputFormat format = new OutputFormat(" ",true);//创建格式 XMLWriter writer = new XMLWriter(out, format); writer.write(doc); */ //下面为实例 package org.tlgg.test; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import javax.management.relation.Role; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.*; import org.tlgg.bean.Roles; import org.tlgg.bean.Users; import com.sun.beans.decoder.DocumentHandler; public class XMLTest { public static void main(String[] args) throws Exception { /*SAXReader reader = new SAXReader(); //加载Html文档 Document document = reader.read(new File("src/Students.xml")); //获取根节点 Element root = document.getRootElement(); //获取到根节点中的所有子节点 //getElements(root); ListusersList = new ArrayList (); getUsersByXml(root, usersList); for (Users users : usersList) { System.out.println(users); }*/ //------------------------------------------------------- //创建xml文件 List list = new ArrayList (); list.add(new Users(101, "苏小红", "123", 1, new Roles(1,"同学"))); list.add(new Users(102, "苏打绿", "123", 2, new Roles(2,"歌手"))); list.add(new Users(101, "桃子苏", "123", 3, new Roles(3,"食物"))); //创建一个节点 Element root = DocumentHelper.createElement("users"); //以该节点作为根节点创建一个文档 Document doc = DocumentHelper.createDocument(root); for (Users users : list) { Element student = DocumentHelper.createElement("user"); Element id = DocumentHelper.createElement("id"); id.setText(users.getId()+"");//getId返回的是整型,+""变成字符串 Element name = DocumentHelper.createElement("name"); name.setText(users.getName()+""); Element pwd = DocumentHelper.createElement("pwd"); pwd.setText(users.getPassword()+""); Element roleid = DocumentHelper.createElement("roleid"); roleid.setText(users.getRoleid()+""); //创建role节点及其子节点 Element role = DocumentHelper.createElement("role"); Element rid = DocumentHelper.createElement("rid"); rid.setText(users.getRole().getRoleid()+""); Element rname = DocumentHelper.createElement("rname"); rname.setText(users.getRole().getName()+""); role.add(rid); role.add(rname); student.add(id); student.add(name); student.add(pwd); student.add(roleid); student.add(role); root.add(student); } //创建文件字符输出流 FileOutputStream out = new FileOutputStream("src/writeDemo.xml"); //创建格式 OutputFormat format = new OutputFormat(" ",true); //以上面创建的文件字符输出流和格式为参数创建一个 writer XMLWriter writer = new XMLWriter(out, format); writer.write(doc); System.out.println("over"); } public static void getUsersByXml(Element root,List usersList){ //获取当前节点的所有子节点 List elements = root.elements(); for (Element element : elements) { if(element.getName().equals("users")){ Users user = setUser(element); usersList.add(user); } } } //解析当前节点,获取值给User对象 public static Users setUser(Element root){ Users user = new Users(); List eList = root.elements(); for (Element element : eList) { if(element.getName().equals("id")) user.setId(Integer.parseInt(element.getText())); if(element.getName().equals("name")) user.setName(element.getText()); if(element.getName().equals("pwd")) user.setPassword(element.getText()); if(element.getName().equals("roleid")) user.setRoleid(Integer.parseInt(element.getText())); if(element.getName().equals("role")) user.setRole(getRolesByXml(element)); } return user; } //从xml文件中获取role对象 public static Roles getRolesByXml(Element root){ Roles role = new Roles(); List elements=root.elements(); for (Element element : elements) { if(element.getName().equals("id")) role.setRoleid(Integer.parseInt(element.getText())); if(element.getName().equals("name")) role.setName(element.getText()); } return role; } //遍历获取所有节点 public static void getElements(Element root){ List eList = root.elements(); for (Element element : eList) { System.out.println(element.getName()+":"+element.getText().trim()); if(element.attributeValue("class")!=null) System.out.println("class="+element.attributeValue("class")); getElements(element); } } }