【慕课笔记】4-5 应用DOM4J及JDOM方式解析XML—在JDOM中存储Book对象

package com.imooc.xml;  


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;


public class TestJDOM {
	private static ArrayList<Book> bookslist=new ArrayList<Book>();
	public static void main(String[] args) {
	//进行对book.xml文件的JDOM解析
	//准备工作
		//1.创建一个SAXBuilder对象
		SAXBuilder saxBuilder=new SAXBuilder();
		//2.创建一个输入流,将xml文件加载到输入流中
		InputStream in;
		try {
			in = new FileInputStream("src/res/haha.xml");
			//3.通过saxBuilder的build方法,将输入流加载到saxBuilder中
			Document document=saxBuilder.build(in);
			//4.通过document对象获取xml文件的根节点
			Element rootelement=document.getRootElement();
			//5.获取根节点下的子节点的List集合
			List<Element> booklist=rootelement.getChildren();
			//继续进行解析
			for(Element book:booklist){
				Book bookentity=new Book();
				System.out.println("-----开始解析第"+(booklist.indexOf(book)+1)+"本书-----");
				//解析book的属性集合
				List<Attribute> attrlist=book.getAttributes();
				//知道节点下属性名称是,获取节点值
//				book.getAttributeValue("id");
				//遍历attrlist(针对不清楚book节点下属性的名字和数量)
				for(Attribute attr:attrlist){
					//获取属性名
					String attrname=attr.getName();
					//获取属性值
					String attrvalue=attr.getValue();
					System.out.println("属性名:"+attrname+"  属性值:"+attrvalue);
					if(attrname.equals("id")){
						bookentity.setId(attrvalue);
					}
				}
				
				//对book节点的子节点的节点名及节点值的遍历
				List<Element> bookchilds=book.getChildren();
				for(Element child:bookchilds){
					System.out.println("节点名:"+child.getName()+"  结点值:"+child.getValue());
					if(child.getName().equals("name")){
						bookentity.setName(child.getValue());
					}else if(child.getName().equals("author")){
						bookentity.setAuthor(child.getValue());
					}else if(child.getName().equals("price")){
						bookentity.setPrice(child.getValue());
					}else if(child.getName().equals("year")){
						bookentity.setYear(child.getValue());
					}else if(child.getName().equals("language")){
						bookentity.setLanguage(child.getValue());
					}
						
				}
				System.out.println("-----结束解析第"+(booklist.indexOf(book)+1)+"本书-----");
				bookslist.add(bookentity);
				bookentity=null;
				System.out.println(bookslist.size());
				
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值