Dom4j解析xml

dom4j是当前解析xml文件最好的方式之一。

为了熟练一下dom4j的使用,特做以下尝试:

1.books.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
	<book id="1">
		<name>Java从入门到放弃</name>
		<author>杰克李</author>
		<year>2010</year>
		<price>1024</price>
	</book>
	<book id="2">
		<name>腰椎间盘突出恢复治疗</name>
		<author>詹妮杨</author>
		<year>2010</year>
		<price>1024</price>
	</book>
	<book id="3">
		<name>护发育发</name>
		<author>wangba</author>
		<year>2010</year>
		<price>1024</price>
	</book>
</bookstore>

2.bean Book.java就不粘了。

3.按照xml中标签的之间的关系 解析xml

package com.lune.myReadXmlUtils;

import com.lune.bean.Book;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author yhcookie
 */

/**
 * 1.首先new一个SAXReader 用来读取file
 * 2.用SAXReader的实例 Document document = reader.read(file) 这是入参的file【File bookfile = new File("src/res/books.xml");】
 * 3.用返回的document的getRootElement()方法 获取根标签
 * 4.根标签的elementIterator()方法 获取子标签的迭代器
 * 5.使用迭代器 迭代出子标签 
 * 6.再用 attributes()或者elementIterator()方法拿到想要的数据 封装到bean中
 * 【
 *      attribute.getName() 获取属性名,返回String
 *      attribute.getValue() 获取属性值,返回String
 *      
 *      element.getName() 获取标签名
 *      element.getStringValue() 获取该标签的文本值 		
 *          ey:<year>2010</year>   
 *              标签名为 "year"
 *              标签文本值为 "2010"
 * 】
 */
public class MyReadXMLByDom4j {

    public static List<Book> getBookByDom4j(File file){
        SAXReader reader = new SAXReader();
        List<Book> list = new ArrayList<>();
        try {
            //用saxReader把 file读成一个document
            Document document = reader.read(file);
            //用获取到的这个document获取根节点元素
            Element rootElement = document.getRootElement();
            System.out.println("马上打印的是根节点");
            System.out.println(rootElement);
            System.out.println("获取的是根节点的下边的子节点的iterator");
            //得到一个和含有booklist的迭代器
            Iterator iterator = rootElement.elementIterator();
            while (iterator.hasNext()){
                //迭代这个东西 先拿到每个book标签的属性 id
                Book book = new Book();
                Element element = (Element) iterator.next();
                //	attributes是为了拿到这一层标签的属性<book id="1"></book>
                List<Attribute> attributes = element.attributes();
                for (Attribute attribute : attributes) {
                    if("id".equals(attribute.getName())){
                        String idValue = attribute.getValue();
                        book.setId(Integer.parseInt(idValue));
                    }
                }
                //现在 再迭代每个book标签呢底下的元素
                // elementIterator是为了拿到这一层标签的子标签的集合的迭代器
                Iterator iterator1 = element.elementIterator();
                while(iterator1.hasNext()){
                    Element element1 = (Element) iterator1.next();
                    String name = element1.getName();
                    String stringValue = "";
                    if("name".equals(name)){
                        stringValue = element1.getStringValue();
                        book.setName(stringValue);
                    }else if("author".equals(name)){
                        stringValue = element1.getStringValue();
                        book.setAuthor(stringValue);
                    }else if("price".equals(name)){
                        stringValue = element1.getStringValue();
                        book.setPrice(Double.parseDouble(stringValue));
                    }else if("year".equals(name)){
                        stringValue = element1.getStringValue();
                        book.setYear(Integer.parseInt(stringValue));
                    }
                }
                list.add(book);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return list;
    }

    public static void main(String[] args) {
        File bookfile = new File("src/res/books.xml");
        List<Book> bookByDom4j = getBookByDom4j(bookfile);
        for (Book book : bookByDom4j) {
            System.out.println(book);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值