使用SAXReader解析xml文件(通过dom4j方式)

我们在读取外部接口时,处理json方式,不可避免的还会遇到xml文件,当遇到xml文件时,通常用到的解析方式大概有四种:
1.DOM方式解析XML
2.SAX方式解析XML
3.JDOM方式解析XML
4.DOM4j方式解析XML
在这四种方式中第四种是第三种的分支,功能非常强大,在这里简要介绍一下第四种方式
具体代码如下:
创建xml文件



    
        冰与火之歌
        乔治马丁
        2014
        89
    
    
        安徒生童话
        安徒生
        2004
        77
    
    
        think think think
        aaa
        1997
        100
    
    
        围城
        钱钟书
        1997
        50
    

创建实体

package com.modou.workday.domain;

public class Book {

	private int id;
    private String name;
    private String author;
    private int year;
    private double price;
	
	
	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Book(int id, String name, String author, int year, double price) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
		this.year = year;
		this.price = 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 String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "getBookXml [id=" + id + ", name=" + name + ", author=" + author + ", year=" + year + ", price="
				+ price + "]";
	}
    
}

DOM4J解析XML

package com.modou.workday.util;

import java.io.File;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class getBookXml {
	
	public static void ReaderDon4j(File file) throws DocumentException {
		// 创建SAXReader对象
		SAXReader reader = new SAXReader();
		// 读取文件
		Document document = reader.read(file);
		// 获取根元素
		Element root = document.getRootElement();
		// 获取根元素下面的所有元素
		List list = root.elements();
		for (Element element : list) {
			List child = element.elements();
			//得到当前节点的属性对象
			List attributes=element.attributes();
			for (Attribute attribute : attributes) {
				System.out.println(attribute);
				if (attribute.getName().equals("id")) {
					String id = attribute.getValue();
					System.out.println(id);
				}
			}
			for (Element element2 : child) {
				String name = element2.getName();
				String text = element2.getText();
				System.out.println(name + "---" + text);
			}
		}
}
	public static void main(String[] args) throws DocumentException {
	    File file = new File("C:\\Users\\cxj\\Desktop\\workday\\xml\\book.xml");
	    ReaderDon4j(file);
	}
}

结果

org.dom4j.tree.DefaultAttribute@627551fb [Attribute: name id value "1"]
name---冰与火之歌
author---乔治马丁
year---2014
price---89
org.dom4j.tree.DefaultAttribute@2b552920 [Attribute: name id value "2"]
name---安徒生童话
author---安徒生
year---2004
price---77
org.dom4j.tree.DefaultAttribute@2758fe70 [Attribute: name id value "3"]
name---think think think
author---aaa
year---1997
price---100
org.dom4j.tree.DefaultAttribute@1f36e637 [Attribute: name id value "4"]
name---围城
author---钱钟书
year---1997
price---50

你可能感兴趣的:(接口)