一、dom4j 读写改操作,先导入jar包-1
例子1:
1 //dom4j读取 2 SAXReader reader = new SAXReader(); 3 //文档对象 4 Document document = reader.read(new File("F:\yujun\javaweb\第一章\student.xml")); 5 //根节点对象 6 Element root = document.getRootElement(); 7 //取得根节点的所有子节点集合 8 List<Element> chs = root.elements(); 9 10 for(Element ch : chs) { 11 String no = ch.attributeValue("no"); //取得属性值 12 String name = ch.elementText("name");//取得子节点的文本值 13 String sex = ch.elementText("sex");//取得子节点的文本值 14 String age = ch.elementText("age");//取得子节点的文本值 15 16 System.out.println("学号:"+no+",姓名:"+name+",性别:"+sex+",年龄:"+age); 17 }
例子2:
1 //dom4j创建新xml文件 2 //文档 3 Document document = DocumentHelper.createDocument(); 4 //根节点 5 Element root = DocumentHelper.createElement("stus"); 6 //把根节点放入文档对象中 7 document.setRootElement(root); 8 9 //创建stu的子节点 10 Element stu = root.addElement("stu"); 11 stu.addAttribute("no", "001"); //添加属性 12 //给stu节点创建子节点 13 Element name = stu.addElement("name"); 14 Element sex = stu.addElement("sex"); 15 Element age = stu.addElement("age"); 16 //给子节点赋值 17 name.setText("张三"); 18 sex.setText("女"); 19 age.setText("20"); 20 21 //输出 22 XMLWriter writer = new XMLWriter(new FileOutputStream(new File("F:\\yujun\\javaweb\\第一章\\student3.xml"))); 23 writer.write(document); 24 writer.close(); 25 System.out.println("创建成功");
例子3:
1 //dom4j给xml文件添加节点数据 2 3 //reader 4 SAXReader reader = new SAXReader(); 5 //文档对象 6 Document document = reader.read(new File("F:\\\\yujun\\\\javaweb\\\\第一章\\\\student3.xml")); 7 //根节点 8 Element root = document.getRootElement(); 9 10 //创建子节点(stu) 11 Element stu = root.addElement("stu"); 12 //创建孙子节点 13 Element name = stu.addElement("name"); 14 Element sex = stu.addElement("sex"); 15 Element age = stu.addElement("age"); 16 //赋值 17 stu.addAttribute("no", "002"); 18 name.setText("李四"); 19 sex.setText("女"); 20 age.setText("30"); 21 22 //把根节点重置到文档对象中 23 document.setRootElement(root); 24 25 //输出 26 XMLWriter writer = new XMLWriter(new FileOutputStream(new File("F:\\yujun\\javaweb\\第一章\\student3.xml"))); 27 writer.write(document); 28 writer.close(); 29 System.out.println("添加成功");
例子4:
1 //使用deom4j修改xml文件数据 2 3 SAXReader reader = new SAXReader(); 4 Document document = reader.read(new File("F:\yujun\javaweb\第一章\student3.xml")); 5 Element root = document.getRootElement(); 6 7 List<Element> chs = root.elements(); 8 //stu 9 for(Element ch : chs) { 10 String no = ch.attributeValue("no"); 11 if("002".equals(no)) { 12 Element age = ch.element("age");//取得孩子节点 13 age.setText("40"); 14 15 //删除节点 16 //root.remove(ch); 17 18 break; 19 } 20 } 21 22 //把根节点重置到文档对象中 23 document.setRootElement(root); 24 25 //输出 26 XMLWriter writer = new XMLWriter(new FileOutputStream(new File("F:\yujun\javaweb\第一章\student3.xml"))); 27 writer.write(document); 28 writer.close(); 29 System.out.println("修改成功");
二、jdom 读写改操作,先导入jar包-1
例子1:
1 //使用jdom解析mxl 2 3 //创建builder对象 4 SAXBuilder builder = new SAXBuilder(); 5 6 //通过xml文件构建文档对象 7 Document document = builder.build(new File("F:\yujun\javaweb\第一章\student.xml")); 8 9 10 //获取文档中的根节点对象 11 Element root = document.getRootElement(); 12 13 //获取根节点下的所有子节点对象集合 14 List<Element> chs = root.getChildren(); 15 16 17 for(Element ch : chs) { 18 //获取属性的值 19 String no = ch.getAttributeValue("no"); 20 //获取子节点的文本值 21 String name = ch.getChildText("name"); 22 String sex = ch.getChildText("sex"); 23 String age = ch.getChildText("age"); 24 25 /*Element cla = ch.getChild("class"); 26 String claId = cla.getChildText("id"); 27 String claName = cla.getChildText("name");*/ 28 29 //System.out.println("学号:"+no+",姓名:"+name+",性别:"+sex+",年龄:"+age+",班级id:"+claId+",班级名次:"+claName); 30 System.out.println("学号:"+no+",姓名:"+name+",性别:"+sex+",年龄:"+age); 31 }
例子2:
1 //使用jdom创建新xml文件 2 3 //创建各给节点 4 Element stu = new Element("stu"); 5 Element name = new Element("name"); 6 Element sex = new Element("sex"); 7 Element age = new Element("age"); 8 9 //给节点赋值 10 name.setText("赵六"); 11 sex.setText("女"); 12 age.setText("40"); 13 14 //关系 15 stu.addContent(name); 16 stu.addContent(sex); 17 stu.addContent(age); 18 stu.setAttribute("no", "004"); //设置属性 19 20 //根节点 21 Element stus = new Element("stus"); 22 stus.addContent(stu); 23 Document document = new Document(stus); 24 25 //格式化对象 26 Format format = Format.getCompactFormat(); 27 format.setIndent(" ");//缩进 28 29 30 //输出对象 31 XMLOutputter out = new XMLOutputter(format); 32 out.output(document, new FileOutputStream(new File("F:\yujun\javaweb\第一章\student2.xml"))); 33 System.out.println("创建成功");
例子3:
1 //使用jdom给xml文件添加节点数据 2 3 //builder对象 4 SAXBuilder builder = new SAXBuilder(); 5 //文档对象 6 Document document = builder.build(new File("F:\\yujun\\javaweb\\第一章\\student2.xml")); 7 8 //创建节点数据 9 Element stu = new Element("stu"); 10 Element name = new Element("name"); 11 Element sex = new Element("sex"); 12 Element age = new Element("age"); 13 //给节点赋值 14 name.setText("abc"); 15 sex.setText("女"); 16 age.setText("50"); 17 stu.setAttribute("no","006"); 18 //关系 19 stu.addContent(name); 20 stu.addContent(sex); 21 stu.addContent(age); 22 23 //获取根节点 24 Element root = document.getRootElement(); 25 root.addContent(stu); 26 27 //把root节点更新到文档中 28 document.setContent(root); 29 30 //输出对象 31 XMLOutputter out = new XMLOutputter(); 32 out.output(document, new FileOutputStream(new File("F:\\\\yujun\\\\javaweb\\\\第一章\\\\student2.xml"))); 33 System.out.println("添加节点数据成功");
例子4:
1 //使用jdom修改节点数据 2 //修改学号006的性别为:男 3 4 //builder对象 5 SAXBuilder builder = new SAXBuilder(); 6 Document document = builder.build(new File("F:\\\\yujun\\\\javaweb\\\\第一章\\\\student2.xml")); 7 8 //根节点 9 Element root = document.getRootElement(); 10 List<Element> chs = root.getChildren(); 11 for(Element ch : chs) { 12 String no = ch.getAttributeValue("no"); 13 if("006".equals(no)) { 14 Element sex = ch.getChild("sex"); 15 sex.setText("男"); 16 17 //删除 18 //root.removeContent(ch); 19 20 break; 21 } 22 } 23 24 document.setContent(root); 25 XMLOutputter out = new XMLOutputter(); 26 out.output(document, new FileOutputStream(new File("F:\\\\yujun\\\\javaweb\\\\第一章\\\\student2.xml"))); 27 System.out.println("修改成功");