1. 程式人生 > >java 生成和解析xml

java 生成和解析xml

img use public set odin 實現 etag system Coding

本文主要使用的是Jdom.jar包(包的下載百度一下)實現了生成xml文件和解析xml文件

下面是生成xml的實現

說明:stuLists集合是一個存放著Student對象的集合

 1 import java.io.File;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.util.ArrayList;
 6 
 7 import org.jdom.Document;
 8 import org.jdom.Element;
9 import org.jdom.input.SAXBuilder; 10 import org.jdom.output.Format; 11 import org.jdom.output.XMLOutputter; 12 13 public class AddXml { 14 public static void main(String[] args) { 15 new AddXml().changeXml(); 16 } 17 public void changeXml(){ 18 Jdbc jdbc = new
Jdbc(); 19 jdbc.addList(); 20 ArrayList<Student> stuLists = Jdbc.getStuList(); 21 22 Document docu = new Document(); 23 Element root = new Element("root"); 24 docu.addContent(root); 25 for (int i = 0; i < stuLists.size(); i++) { 26 // System.out.println(stuLists.get(i));
27 Student s = stuLists.get(i); 28 29 Element info = new Element("info"); 30 Element student = new Element("student"); 31 Element id = new Element("id"); 32 Element name = new Element("name"); 33 Element sex = new Element("sex"); 34 Element age = new Element("age"); 35 36 Element book = new Element("book"); 37 Element bid = new Element("bid"); 38 Element bname = new Element("bname"); 39 Element bprice = new Element("bprice"); 40 Element bautor = new Element("bautor"); 41 book.addContent(bid); 42 book.addContent(bname); 43 book.addContent(bprice); 44 book.addContent(bautor); 45 46 student.addContent(id); 47 student.addContent(name); 48 student.addContent(sex); 49 student.addContent(age); 50 info.addContent(student); 51 info.addContent(book); 52 53 root.addContent(info); 54 int a = i+1; 55 String No = "000"+a; 56 student.setAttribute("No", No); 57 id.setText(s.getId()); 58 name.setText(s.getName()); 59 sex.setText(s.getSex()); 60 age.setText(s.getAge()); 61 62 String b="0"+a; 63 bid.setText(b); 64 bname.setText("java核心"); 65 bprice.setText("1334.0"); 66 bautor.setText("star"); 67 68 } 69 //格式化生成的xml文件,如果不進行格式化的話,生成的xml文件將會是很長的一行... 70 Format format = Format.getCompactFormat(); 71 format.setEncoding("utf-8"); 72 format.setIndent(" "); 73 XMLOutputter xo = new XMLOutputter(format); 74 try { 75 xo.output(docu, new FileOutputStream(new File("e:/io/stu.xml"))); 76 } catch (FileNotFoundException e) { 77 // TODO Auto-generated catch block 78 e.printStackTrace(); 79 } catch (IOException e) { 80 // TODO Auto-generated catch block 81 e.printStackTrace(); 82 } 83 System.out.println("生成xml文件成功!!!"); 84 } 85 }

結果如圖所示:

技術分享

技術分享

下面是用java解析上面所寫的xml文件

簡寫版

 1 package com.direct.demo;
 2 
 3 import java.io.IOException;
 4 import java.util.List;
 5 
 6 import org.jdom.Document;
 7 import org.jdom.Element;
 8 import org.jdom.JDOMException;
 9 import org.jdom.input.SAXBuilder;
10 
11 public class Saxxml {
12     
13     public static void main(String[] args) {
14         //解析xml文檔
15         SAXBuilder builder =  new SAXBuilder();
16         Document  docu = null;
17         try {
18             docu = builder.build("e:/io/stu.xml");
19         } catch (JDOMException e) {
20             e.printStackTrace();
21         } catch (IOException e) {
22             e.printStackTrace();
23         }
24         
25          Element root = docu.getRootElement();//得到根目錄
26          List stulist = root.getChildren();
27          System.out.println(stulist.size()+"-----------");
28          System.out.println("------------------讀取xml文檔的信息---------------------");
29         for (int i = 0; i < stulist.size(); i++) {
30             Element e = (Element) stulist.get(i); 
31             String stuid;
32             String stuname ;
33             String stusex ;
34             String stuage;
35             if (i==0) {
36                  stuid = e.getChildText("stuid");
37                 stuname = e.getChildText("stuname");
38                 stusex = e.getChildText("stusex");
39                 stuage = e.getChildText("stuage");
40             }else {
41                 stuid = e.getChildText("stuid"+i);
42                 stuname = e.getChildText("stuname"+i);
43                 stusex = e.getChildText("stusex"+i);
44                  stuage = e.getChildText("stuage"+i);    
45             }
46             System.out.println("屬性:"+e.getAttributeValue("No"));
47             System.out.println("學號:"+stuid);
48             System.out.println("姓名:"+stuname);
49             System.out.println("年齡:"+stuage);
50             System.out.println("性別:"+stusex);
51             System.out.println("--------------------");
52         }
53     }    
54 }

java 生成和解析xml