JDOM解析XML文件

先导入jdom-2.0.6.jar包


     	// 1.创建一个SAXBuilder的对象
	   SAXBuilder saxBuilder = new SAXBuilder();
	   // 2.创建一个输入流,将xml文件加载到输入流中
		in = new FileInputStream("src/res/books.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节点指定属性名称节点值
//				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("year")) {
						bookEntity.setYear(child.getValue());
					}else if(child.getName().equals("price")) {
						bookEntity.setPrice(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());
				System.out.println(booksList.get(0).getName());
			}

完整代码

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

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

import entity.Book;


public class JDOMTest {
	private static ArrayList<Book> booksList = new ArrayList<Book>();
	public static void main(String[] args) {
		// 1.创建一个SAXBuilder的对象
		SAXBuilder saxBuilder = new SAXBuilder();
		InputStream in;
		try {
			// 2.创建一个输入流,将xml文件加载到输入流中
			in = new FileInputStream("src/res/books.xml");
			InputStreamReader isr = new InputStreamReader(in, "UTF-8");
			// 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节点指定属性名称节点值
//				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("year")) {
						bookEntity.setYear(child.getValue());
					}else if(child.getName().equals("price")) {
						bookEntity.setPrice(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());
				System.out.println(booksList.get(0).getName());
			}
		} 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();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值