Dom解析是将xml文件全部载入,组装成一颗dom树,然后通过节点以及节点之间的关系来解析xml文件,下面结合这个xml文件来进行dom解析。

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <books>  
  3.     <book id="12">  
  4.         <name>thinking in java</name>  
  5.         <price>85.5</price>  
  6.     </book>  
  7.     <book id="15">  
  8.         <name>Spring in Action</name>  
  9.         <price>39.0</price>  
  10.     </book>  
  11. </books>  

在这里当我们得到节点book时,也就是图中1所画的地方,如果我们调用它的getChildNodes()方法,大家猜猜它的子节点有几个?不包括它的孙子节点,thinking in java这种的除外,因为它是孙子节点。它总共有5个子节点,分别是图中2、3、4、5、6所示的那样。所以在解析时,一定要小心,不要忽略空白的地方。

然后看代码来解析book.xml文件

DomParseService.java

    1. import java.io.InputStream;   
    2. import java.util.ArrayList;   
    3. import java.util.List;   
    4.   
    5. import javax.xml.parsers.DocumentBuilder;   
    6. import javax.xml.parsers.DocumentBuilderFactory;   
    7.   
    8. import org.w3c.dom.Document;   
    9. import org.w3c.dom.Element;   
    10. import org.w3c.dom.NodeList;   
    11. import org.w3c.dom.Node;   
    12.   
    13. import com.xtlh.cn.entity.Book;   
    14.   
    15. public class DomParseService {   
    16.     public List<Book> getBooks(InputStream inputStream) throws Exception{   
    17.         List<Book> list = new ArrayList<Book>();   
    18.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
    19.         DocumentBuilder builder = factory.newDocumentBuilder();   
    20.         Document document = builder.parse(inputStream);   
    21.         Element element = document.getDocumentElement();   
    22.            
    23.         NodeList bookNodes = element.getElementsByTagName("book");   
    24.         for(int i=0;i<bookNodes.getLength();i++){   
    25.             Element bookElement = (Element) bookNodes.item(i);   
    26.             Book book = new Book();   
    27.             book.setId(Integer.parseInt(bookElement.getAttribute("id")));   
    28.             NodeList childNodes = bookElement.getChildNodes();   
    29. //          System.out.println("*****"+childNodes.getLength());   
    30.             for(int j=0;j<childNodes.getLength();j++){   
    31.                 if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE){   
    32.                     if("name".equals(childNodes.item(j).getNodeName())){   
    33.                         book.setName(childNodes.item(j).getFirstChild().getNodeValue());   
    34.                     }else if("price".equals(childNodes.item(j).getNodeName())){   
    35.                         book.setPrice(Float.parseFloat(childNodes.item(j).getFirstChild().getNodeValue()));   
    36.                     }   
    37.                 }   
    38.             }//end for j   
    39.             list.add(book);   
    40.         }//end for i   
    41.         return list;   
    42.     }   
    43. }  
    1. public class Book {   
    2.     private int id;   
    3.     private String name;   
    4.     private float price;   
    5.     public int getId() {   
    6.         return id;   
    7.     }   
    8.     public void setId(int id) {   
    9.         this.id = id;   
    10.     }   
    11.     public String getName() {   
    12.         return name;   
    13.     }   
    14.     public void setName(String name) {   
    15.         this.name = name;   
    16.     }   
    17.     public float getPrice() {   
    18.         return price;   
    19.     }   
    20.     public void setPrice(float price) {   
    21.         this.price = price;   
    22.     }   
    23.     @Override  
    24.     public String toString(){   
    25.         return this.id+":"+this.name+":"+this.price;   
    26.     }   
    27. }  
    1. public class ParseTest extends TestCase{   
    2.   
    3.     public void testDom() throws Exception{   
    4.         InputStream input = this.getClass().getClassLoader().getResourceAsStream("book.xml");   
    5.         DomParseService dom = new DomParseService();   
    6.         List<Book> books = dom.getBooks(input);   
    7.         for(Book book : books){   
    8.             System.out.println(book.toString());   
    9.         }   
    10.     }   
    11. }  

 

    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值