JAVA解析XML文件(DOM,SAX,JDOM,DOM4j附代码实现)

1、解析XML主要有四种方式

1.DOM方式解析XML(与平台无关,JAVA提供,一次性加载XML文件内容,形成树结构,不适用于大文件)
2.SAX方式解析XML(基于事件驱动,逐条解析,适用于只处理XML数据,不易编码)
3.JDOM方式解析XML(使用具体类而不使用接口)
4.DOM4j方式解析XML(推荐)

2、代码实现

(1)XML文件

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <bookstore>  
 3     <book id="1">  
 4         <name>平凡的世界</name>  
 5         <author>路遥</author>  
 6         <year>2017</year>  
 7         <price>77</price>  
 8     </book>  
 9     <book id="2">  
10         <name>百年孤独</name>  
11         <author>加西亚·马尔克斯</author>  
12         <year>1967</year>  
13         <price>80</price>  
14     </book>  
15     <book id="3">  
16         <name>程序员的修炼之道</name>  
17         <author>安德鲁·亨特、大卫·托马斯</author>  
18         <year>2011</year>  
19         <price>156</price>  
20     </book>  
21 </bookstore>

(2)XML对应的Bean类

 1 public class Book {   
 2       
 3     private int id;  
 4     private String name;  
 5     private String author;  
 6     private int year;  
 7     private double price;  
 8       
 9     /** 
10      * @return the id 
11      */  
12     public int getId() {  
13         return id;  
14     }  
15     /** 
16      * @param id the id to set 
17      */  
18     public void setId(int id) {  
19         this.id = id;  
20     }  
21     /** 
22      * @return the name 
23      */  
24     public String getName() {  
25         return name;  
26     }  
27     /** 
28      * @param name the name to set 
29      */  
30     public void setName(String name) {  
31         this.name = name;  
32     }  
33     /** 
34      * @return the author 
35      */  
36     public String getAuthor() {  
37         return author;  
38     }  
39     /** 
40      * @param author the author to set 
41      */  
42     public void setAuthor(String author) {  
43         this.author = author;  
44     }  
45     /** 
46      * @return the year 
47      */  
48     public int getYear() {  
49         return year;  
50     }  
51     /** 
52      * @param year the year to set 
53      */  
54     public void setYear(int year) {  
55         this.year = year;  
56     }  
57     /** 
58      * @return the price 
59      */  
60     public double getPrice() {  
61         return price;  
62     }  
63     /** 
64      * @param price the price to set 
65      */  
66     public void setPrice(double price) {  
67         this.price = price;  
68     }  
69       
70     @Override  
71     public String toString() {  
72         return "Book [id=" + id + ", name=" + name + ", author=" + author + ", year=" + year + ", price=" + price + "]";  
73     }  
74           
75 }  

3、XML解析

(1)DOM方式解析XML

 1 public class ReadxmlByDom {  
 2     private static DocumentBuilderFactory dbFactory = ;  
 3     private static DocumentBuilder db = ;  
 4     private static Document document = ;  
 5     private static List<Book> books = ;  
 6     static{  
 7         try {  
 8             dbFactory = DocumentBuilderFactory.newInstance();  
 9             db = dbFactory.newDocumentBuilder();  
10         } catch (ParserConfigurationException e) {  
11             e.printStackTrace();  
12         }  
13     }  
14       
15     public static List<Book> getBooks(String fileName) throws Exception{  
16         //将给定 URI 的内容解析为一个 XML 文档,并返回Document对象  
17         document = db.parse(fileName);  
18         //按文档顺序返回包含在文档中且具有给定标记名称的所有 Element 的 NodeList  
19         NodeList bookList = document.getElementsByTagName("book");  
20         books = new ArrayList<Book>();  
21         //遍历books  
22         for(int i=0;i<bookList.getLength();i++){  
23             Book book = new Book();  
24             //获取第i个book结点  
25             org.w3c.dom.Node node = bookList.item(i);  
26             //获取第i个book的所有属性  
27             NamedNodeMap namedNodeMap = node.getAttributes();  
28             //获取已知名为id的属性值  
29             String id = namedNodeMap.getNamedItem("id").getTextContent();//System.out.println(id);  
30             book.setId(Integer.parseInt(id));  
31               
32             //获取book结点的子节点,包含了Test类型的换行  
33             NodeList cList = node.getChildNodes();//System.out.println(cList.getLength());9  
34               
35             //将一个book里面的属性加入数组  
36             ArrayList<String> contents = new ArrayList<>();  
37             for(int j=1;j<cList.getLength();j+=2){  
38                   
39                 org.w3c.dom.Node cNode = cList.item(j);  
40                 String content = cNode.getFirstChild().getTextContent();  
41                 contents.add(content);  
42                 //System.out.println(contents);  
43             }  
44               
45             book.setName(contents.get(0));  
46             book.setAuthor(contents.get(1));  
47             book.setYear(Integer.parseInt(contents.get(2)));  
48             book.setPrice(Double.parseDouble(contents.get(3)));  
49             books.add(book);  
50         }  
51           
52         return books;  
53           
54     }  
55       
56     public static void main(String args[]){  
57         String fileName = "src/res/books.xml";  
58         try {  
59             List<Book> list = ReadxmlByDom.getBooks(fileName);  
60             for(Book book :list){  
61                 System.out.println(book);  
62             }  
63         } catch (Exception e) {  
64             // TODO Auto-generated catch block  
65             e.printStackTrace();  
66         }  
67     }  
68           
69 }  

(2)SAX方式解析XML(比较麻烦,需要配置一个handler处理器)

DefaultHandler处理器

  1 /** 
  2  * 用SAX解析xml文件时需要的handler 
  3  */  
  4 public class SAXParseHandler extends DefaultHandler {  
  5     private List<Book> list;         //存放解析到的book数组  
  6     private Book book;               //存放当前解析的book  
  7       
  8     private String content = ;   //存放当前节点值  
  9       
 10     /** 
 11      * 开始解析xml文档时调用此方法 
 12      */  
 13     @Override  
 14     public void startDocument() throws SAXException {  
 15           
 16         super.startDocument();  
 17         System.out.println("开始解析xml文件");  
 18         list = new ArrayList<Book>();  
 19     }  
 20   
 21   
 22   
 23     /**  
 24      * 文档解析完成后调用此方法 
 25      */  
 26     @Override  
 27     public void endDocument() throws SAXException {  
 28           
 29         super.endDocument();  
 30         System.out.println("xml文件解析完毕");  
 31     }  
 32   
 33   
 34   
 35     /** 
 36      * 开始解析节点时调用此方法 
 37      */  
 38     @Override  
 39     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {  
 40           
 41         super.startElement(uri, localName, qName, attributes);  
 42           
 43         //当节点名为book时,获取book的属性id  
 44         if(qName.equals("book")){  
 45             book = new Book();  
 46             String id = attributes.getValue("id");//System.out.println("id值为"+id);  
 47             book.setId(Integer.parseInt(id));  
 48         }  
 49           
 50     }  
 51   
 52   
 53     /** 
 54      *节点解析完毕时调用此方法 
 55      * 
 56      *@param qName 节点名 
 57      */  
 58     @Override  
 59     public void endElement(String uri, String localName, String qName) throws SAXException {  
 60           
 61         super.endElement(uri, localName, qName);  
 62         if(qName.equals("name")){  
 63             book.setName(content);  
 64             //System.out.println("书名"+content);  
 65         }else if(qName.equals("author")){  
 66             book.setAuthor(content);  
 67         //  System.out.println("作者"+content);  
 68         }else if(qName.equals("year")){  
 69             book.setYear(Integer.parseInt(content));  
 70         //  System.out.println("年份"+content);  
 71         }else if(qName.equals("price")){  
 72             book.setPrice(Double.parseDouble(content));  
 73         //  System.out.println("价格"+content);  
 74         }else if(qName.equals("book")){         //当结束当前book解析时,将该book添加到数组后置为空,方便下一次book赋值  
 75             list.add(book);  
 76             book = ;  
 77         }     
 78           
 79     }  
 80   
 81   
 82   
 83     /**  
 84      * 此方法用来获取节点的值 
 85      */  
 86     @Override  
 87     public void characters(char[] ch, int start, int length) throws SAXException {  
 88           
 89         super.characters(ch, start, length);  
 90           
 91         content = new String(ch, start, length);  
 92         //收集不为空白的节点值  
 93 //      if(!content.trim().equals("")){  
 94 //          System.out.println("节点值为:"+content);  
 95 //      }  
 96           
 97     }  
 98   
 99     public List<Book> getBooks(){  
100         return list;  
101     }  
102       
103 }  

用SAX方式读取xml文件

 1 /** 
 2  * 用SAX方式读取xml文件 
 3  */  
 4 public class ReadXmlBySAX {  
 5   
 6     private static List<Book> books = ;  
 7       
 8     private  SAXParserFactory sParserFactory = ;  
 9     private  SAXParser parser = ;  
10       
11       
12     public List<Book> getBooks(String fileName) throws Exception{  
13         SAXParserFactory sParserFactory = SAXParserFactory.newInstance();  
14         SAXParser parser = sParserFactory.newSAXParser();  
15           
16         SAXParseHandler handler = new SAXParseHandler();  
17         parser.parse(fileName, handler);  
18           
19         return handler.getBooks();  
20           
21     }  
22     /** 
23      * @param args 
24      */  
25     public static void main(String[] args) {  
26         try {  
27             books = new ReadXmlBySAX().getBooks("src/res/books.xml");  
28             for(Book book:books){  
29                 System.out.println(book);  
30             }  
31               
32         } catch (Exception e) {  
33             e.printStackTrace();  
34         }  
35   
36     }  
37   
38 }  

(3)JDOM方式解析XML

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

4、DOM4j方式解析XML

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

猜你喜欢

转载自www.cnblogs.com/zhuziyu/p/8940123.html