Java Dom解析xml
来源:互联网 发布:apache 监控 插件 编辑:程序博客网 时间:2024/04/19 13:57
Java Dom解析xml
Dom解析是将xml文件全部载入,组装成一颗dom树,然后通过节点以及节点之间的关系来解析xml文件,下面结合这个xml文件来进行dom解析。
- <?xml version="1.0" encoding="UTF-8"?>
- <books>
- <book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book>
- </books>
<?xml version="1.0" encoding="UTF-8"?><books><book id="12"><name>thinking in java</name><price>85.5</price></book><book id="15"><name>Spring in Action</name><price>39.0</price></book></books>
然后结合一张图来发现dom解析时需要注意的地方
在这里当我们得到节点book时,也就是图中1所画的地方,如果我们调用它的getChildNodes()方法,大家猜猜它的子节点有几个?不包括它的孙子节点,thinking in java这种的除外,因为它是孙子节点。它总共有5个子节点,分别是图中2、3、4、5、6所示的那样。所以在解析时,一定要小心,不要忽略空白的地方。
然后看代码来解析book.xml文件
DomParseService.java
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import org.w3c.dom.Node;
- import com.xtlh.cn.entity.Book;
- public class DomParseService {
- public List<Book> getBooks(InputStream inputStream) throws Exception{
- List<Book> list = new ArrayList<Book>();
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse(inputStream);
- Element element = document.getDocumentElement();
- NodeList bookNodes = element.getElementsByTagName("book");
- for(int i=0;i<bookNodes.getLength();i++){
- Element bookElement = (Element) bookNodes.item(i);
- Book book = new Book();
- book.setId(Integer.parseInt(bookElement.getAttribute("id")));
- NodeList childNodes = bookElement.getChildNodes();
- // System.out.println("*****"+childNodes.getLength());
- for(int j=0;j<childNodes.getLength();j++){
- if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE){
- if("name".equals(childNodes.item(j).getNodeName())){
- book.setName(childNodes.item(j).getFirstChild().getNodeValue());
- }else if("price".equals(childNodes.item(j).getNodeName())){
- book.setPrice(Float.parseFloat(childNodes.item(j).getFirstChild().getNodeValue()));
- }
- }
- }//end for j
- list.add(book);
- }//end for i
- return list;
- }
- }
import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.w3c.dom.Node;import com.xtlh.cn.entity.Book;public class DomParseService {public List<Book> getBooks(InputStream inputStream) throws Exception{List<Book> list = new ArrayList<Book>();DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse(inputStream);Element element = document.getDocumentElement();NodeList bookNodes = element.getElementsByTagName("book");for(int i=0;i<bookNodes.getLength();i++){Element bookElement = (Element) bookNodes.item(i);Book book = new Book();book.setId(Integer.parseInt(bookElement.getAttribute("id")));NodeList childNodes = bookElement.getChildNodes();//System.out.println("*****"+childNodes.getLength());for(int j=0;j<childNodes.getLength();j++){if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE){if("name".equals(childNodes.item(j).getNodeName())){book.setName(childNodes.item(j).getFirstChild().getNodeValue());}else if("price".equals(childNodes.item(j).getNodeName())){book.setPrice(Float.parseFloat(childNodes.item(j).getFirstChild().getNodeValue()));}}}//end for jlist.add(book);}//end for ireturn list;}}
Book.java用来组装数据和盛放数据
- public class Book {
- private int id;
- private String name;
- private float price;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public float getPrice() {
- return price;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- @Override
- public String toString(){
- return this.id+":"+this.name+":"+this.price;
- }
- }
public class Book {private int id;private String name;private float price;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public float getPrice() {return price;}public void setPrice(float price) {this.price = price;}@Overridepublic String toString(){return this.id+":"+this.name+":"+this.price;}}
测试使用单元测试如下ParseTest.java
- public class ParseTest extends TestCase{
- public void testDom() throws Exception{
- InputStream input = this.getClass().getClassLoader().getResourceAsStream("book.xml");
- DomParseService dom = new DomParseService();
- List<Book> books = dom.getBooks(input);
- for(Book book : books){
- System.out.println(book.toString());
- }
- }
- }
- JAVA解析XML DOM
- JAVA解析XML DOM
- JAVA解析XML DOM
- Java Dom解析XML
- JAVA解析XML DOM
- Java Dom解析xml
- Java Dom解析xml
- java dom解析xml
- JAVA dom解析xml
- Java DOM解析XML
- JAVA DOM XML解析
- Java解析XML---DOM
- Java DOM解析XML
- Java Dom解析xml
- java Xml 解析 Dom
- java DOM解析XML
- Java Dom解析xml
- Java Dom解析xml
- 针对SharePoint的C#调试技巧
- 如何在ASP.NET中实现验证码?
- OpenCV使用RANSAC的仿射变换估计 estimateAffine2D
- 锁表头
- 获取屏幕大小
- Java Dom解析xml
- 我的微博和个人网站
- C运算符的优先级与结合
- WIN7下Virtualbox虚拟Ubuntu共享文件夹设置
- git一分钟教程
- Box2D C++ tutorials - Sensors
- 经典的三层设计
- 外业记录:在Windows Azure应用程序中使用基于证书的加密
- 犀利的 oracle 注入技术