DOM4J解析出XML文件中的Product对象

使用DOM4J解析器对product.xml文件进行信息获取,并将获取到的每个商品对象分别使用Product实体对象进行存储,将存储好数据的Product对象封装到List集合中,最终将集合中的所有商品信息通过控制台打印出来。

Product.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<products>
	<product id="001">
			<name>小米手机</name>
			<price>2999</price>
			<type>
				<size>6.0</size>
				<color>红色</color>
			</type>
	</product>
	<product id="002">
			<name>华为手机</name>
			<price>5999</price>
			<type>
				<size>5.8</size>
				<color>黑色</color>
			</type>
	</product>
</products>

Product类文件

package Homework;


public class Product {

	private String name;
	private Integer id;
	private String color;
	private String size;
	private Double price;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "Product [name=" + name + ", id=" + id + ", color=" + color + ", size=" + size + ", price=" + price
				+ "]";
	}

}

主文件ProductTest

package Homework;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
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 ProductTest {
	
	//定义一个存放Product对象的集合
	List<Product> list =new ArrayList();

	//定义解析xml文档方法
	public void getDocument(Element ele,Product pro,Class c) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {
		//获取当前的子节点
		List<Element> child =ele.elements();
		//查看当前子节点是否为Product,如果是表示当前开始获取product数据
		for(Element element : child) {
			//如果节点名为product
			if(element.getName().equals("product")) {
				//创建商品对象
				Product product =new Product();
				//获取当前id属性并且进行赋值
				Attribute att =element.attribute("id");
				//获取当前商品的类描述
				c=product.getClass();
				//获取当前的属性对象
				Field fid=c.getDeclaredField(att.getName());
				
				//调用类型转换属性赋值的方法
				setValues(fid,att.getValue(),product);
				getDocument(element,product,c);
				//将商品对象存放到集合中
				list.add(product);
				
			//如果节点名不是product
			}else {
				//如果节点名不是type
				if(!element.getName().equals("type")) {
					//拿到当前标签名所对应的Product中的属性
					Field field = c.getDeclaredField(element.getName());
					//为该属性赋值
					setValues(field,element.getStringValue(),pro);
				//如果节点吗是type
				}else {
					//递归调用
					getDocument(element,pro,c);
				}
			}
		}
		
	}
	
	
	//定义一个数据存储的方法,完成对象属性的数据存储
	public void setValues(Field fid,String value,Product pro) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		//获取当前属性的数据类型
		Class c =fid.getType();
		//获取当前类型的构造方法对象
		Constructor ctt=c.getDeclaredConstructor(String.class);  //获取带有字符串参数的构造方法对象
		//实例化对象
		Object obj =ctt.newInstance(value);
		//开启保护开关
		fid.setAccessible(true);
		//属性赋值
		fid.set(pro, obj);
	}
	
	
	public static void main(String[] args) throws DocumentException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {
		ProductTest hw=new ProductTest();
		
		//获取DOM4J解析器对象
		SAXReader sr=new SAXReader();
		//获取文档对象
		Document doc =sr.read(new File("src/Homework/product.xml"));		
		//获取根节点
		Element root=doc.getRootElement();
		//调用获取子节点方法
		hw.getDocument(root, null, null);
		
		for(Product pro : hw.list) {
				System.out.println(pro);
		}
	}
	
	
}

posted @ 2021-02-23 20:58  阿伦啊  阅读(129)  评论(0编辑  收藏  举报