1. 程式人生 > >XML兩種讀取和寫入方式的例子:dom4j和jdom

XML兩種讀取和寫入方式的例子:dom4j和jdom

lose 刪除節點 XML 性別 equal bsp ddc 設置 方式

一、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("修改成功");

XML兩種讀取和寫入方式的例子:dom4j和jdom