Java解析XML文件

 

 1 <?xml version=“1.0” encoding=“UTF-8”?>  
 2 <bookstore>  
 3     <book id=“1”>  
 4         <name>冰与火之歌</name>  
 5         <author>乔治马丁</author>  
 6         <year>2014</year>  
 7         <price>89</price>  
 8     </book>  
 9     <book id=“2”>  
10         <name>安徒生童话</name>  
11         <author>安徒生</author>  
12         <year>2004</year>  
13         <price>77</price>  
14     </book>  
15     <book id=“3”>  
16         <name>think think think</name>  
17         <author>aaa</author>  
18         <year>1997</year>  
19         <price>100</price>  
20     </book>  
21 </bookstore>  
 1 public class Book {  
 2       
 3     /** 
 4      * @author lune 
 5      */  
 6       
 7     private int id;  
 8     private String name;  
 9     private String author;  
10     private int year;  
11     private double price;  
12       
13     /** 
14      * @return the id 
15      */  
16     public int getId() {  
17         return id;  
18     }  
19     /** 
20      * @param id the id to set 
21      */  
22     public void setId(int id) {  
23         this.id = id;  
24     }  
25     /** 
26      * @return the name 
27      */  
28     public String getName() {  
29         return name;  
30     }  
31     /** 
32      * @param name the name to set 
33      */  
34     public void setName(String name) {  
35         this.name = name;  
36     }  
37     /** 
38      * @return the author 
39      */  
40     public String getAuthor() {  
41         return author;  
42     }  
43     /** 
44      * @param author the author to set 
45      */  
46     public void setAuthor(String author) {  
47         this.author = author;  
48     }  
49     /** 
50      * @return the year 
51      */  
52     public int getYear() {  
53         return year;  
54     }  
55     /** 
56      * @param year the year to set 
57      */  
58     public void setYear(int year) {  
59         this.year = year;  
60     }  
61     /** 
62      * @return the price 
63      */  
64     public double getPrice() {  
65         return price;  
66     }  
67     /** 
68      * @param price the price to set 
69      */  
70     public void setPrice(double price) {  
71         this.price = price;  
72     }  
73       
74     @Override  
75     public String toString() {  
76         return “Book [id=” + id + “, name=” + name + “, author=” + author + “, year=” + year + “, price=” + price + “]”;  
77     }  
78           
79 }  

1.JDOM方式解析XML

 1 import java.io.FileInputStream;  
 2 import java.io.FileNotFoundException;  
 3 import java.io.IOException;  
 4 import java.util.ArrayList;  
 5 import java.util.List;  
 6   
 7 import org.jdom2.JDOMException;  
 8 import org.jdom2.input.SAXBuilder;  
 9   
10 import com.lune.bean.Book;  
11   
12 import org.jdom2.*;  
13   
14 /** 
15  * 用JDOM方式读取xml文件 
16  * @author lune 
17  */  
18 public class ReadXMLByJDom {  
19       
20     private List<Book> books = null;  
21     private Book book = null;  
22       
23     public List<Book> getBooks(String fileName){  
24         SAXBuilder saxBuilder = new SAXBuilder();  
25         try {  
26             Document document = saxBuilder.build(new FileInputStream(fileName));  
27             //获取根节点bookstore  
28             Element rootElement = document.getRootElement();  
29             //获取根节点的子节点,返回子节点的数组  
30             List<Element> bookList = rootElement.getChildren();  
31             books = new ArrayList<Book>();  
32             for(Element bookElement : bookList){  
33                 book = new Book();  
34                 //获取bookElement的属性  
35                 List<Attribute> bookAttributes = bookElement.getAttributes();  
36                 for(Attribute attribute : bookAttributes){  
37                     if(attribute.getName().equals(“id”)){  
38                         String id = attribute.getValue(); //System.out.println(id);  
39                         book.setId(Integer.parseInt(id));  
40                     }  
41                 }  
42                 //获取bookElement的子节点  
43                 List<Element> children = bookElement.getChildren();  
44                 for(Element child : children){  
45                     if(child.getName().equals(“name”)){  
46                         String name = child.getValue();//System.out.println(name);  
47                         book.setName(name);  
48                     }else if(child.getName().equals(“author”)){  
49                         String author = child.getValue();  
50                         book.setAuthor(author);//System.out.println(author);  
51                     }else if(child.getName().equals(“year”)){  
52                         String year = child.getValue();  
53                         book.setYear(Integer.parseInt(year));  
54                     }else if(child.getName().equals(“price”)){  
55                         String price = child.getValue();  
56                         book.setPrice(Double.parseDouble(price));  
57                     }  
58                       
59                 }  
60                   
61                 books.add(book);  
62                 book = null;  
63                   
64             }  
65               
66         } catch (FileNotFoundException e) {  
67               
68             e.printStackTrace();  
69         } catch (JDOMException e) {  
70               
71             e.printStackTrace();  
72         } catch (IOException e) {  
73               
74             e.printStackTrace();  
75         }  
76           
77         return books;  
78           
79     }  
80   
81       
82     public static void main(String[] args) {  
83         // TODO Auto-generated method stub  
84         String fileName = ”src/res/books.xml”;  
85         List<Book> books= new ReadXMLByJDom().getBooks(fileName);  
86         for(Book book : books){  
87             System.out.println(book);  
88         }  
89     }  
90   
91 }  

2.DOM4j方式解析XML

 1 import java.io.File;  
 2 import java.util.ArrayList;  
 3 import java.util.Iterator;  
 4 import java.util.List;  
 5   
 6 import org.dom4j.Attribute;  
 7 import org.dom4j.Document;  
 8 import org.dom4j.DocumentException;  
 9 import org.dom4j.Element;  
10 import org.dom4j.io.SAXReader;  
11   
12 import com.lune.bean.Book;  
13   
14 /** 
15  * 用DOM4J方法读取xml文件 
16  * @author lune 
17  */  
18 public class ReadXMLByDom4j {  
19       
20     private List<Book> bookList = null;  
21     private Book book = null;  
22       
23     public List<Book> getBooks(File file){  
24           
25         SAXReader reader = new SAXReader();  
26         try {  
27             Document document = reader.read(file);  
28             Element bookstore = document.getRootElement();  
29             Iterator storeit = bookstore.elementIterator();  
30               
31             bookList = new ArrayList<Book>();  
32             while(storeit.hasNext()){  
33                   
34                 book = new Book();  
35                 Element bookElement = (Element) storeit.next();  
36                 //遍历bookElement的属性  
37                 List<Attribute> attributes = bookElement.attributes();  
38                 for(Attribute attribute : attributes){  
39                     if(attribute.getName().equals(“id”)){  
40                         String id = attribute.getValue();//System.out.println(id);  
41                         book.setId(Integer.parseInt(id));  
42                     }  
43                 }  
44                   
45                 Iterator bookit = bookElement.elementIterator();  
46                 while(bookit.hasNext()){  
47                     Element child = (Element) bookit.next();  
48                     String nodeName = child.getName();  
49                     if(nodeName.equals(“name”)){  
50                         //System.out.println(child.getStringValue());  
51                         String name = child.getStringValue();  
52                         book.setName(name);  
53                     }else if(nodeName.equals(“author”)){  
54                         String author = child.getStringValue();  
55                         book.setAuthor(author);  
56                     }else if(nodeName.equals(“year”)){  
57                         String year = child.getStringValue();  
58                         book.setYear(Integer.parseInt(year));  
59                     }else if(nodeName.equals(“price”)){  
60                         String price = child.getStringValue();  
61                         book.setPrice(Double.parseDouble(price));  
62                     }  
63                 }  
64                 bookList.add(book);  
65                 book = null;  
66                   
67             }  
68         } catch (DocumentException e) {  
69           
70             e.printStackTrace();  
71         }  
72           
73           
74         return bookList;  
75           
76     }  
77       
78   
79     /** 
80      * @param args 
81      */  
82     public static void main(String[] args) {  
83         // TODO Auto-generated method stub  
84         File file = new File(“src/res/books.xml”);  
85         List<Book> bookList = new ReadXMLByDom4j().getBooks(file);  
86         for(Book book : bookList){  
87             System.out.println(book);  
88         }  
89     }  
90   
91 }  

转载自:https://blog.csdn.net/zflovecf/article/details/78908788#

posted @ 2019-07-17 10:58  青春-无悔  阅读(198)  评论(0编辑  收藏  举报