Jdom方式j解析xml

来源:互联网 发布:淘宝手机详情空间不够 编辑:程序博客网 时间:2024/06/05 22:34

一、book.xml

<?xml version="1.0" encoding="UTF-8"?><bookstore><book id="1"><name>冰与火之歌</name><author>乔治马丁</author><year>2014</year><price>89</price></book><book id="2"><name>安徒生童话</name><year>2004</year><price>77</price><language>English</language></book></bookstore>

二、测试类:

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 com.imooc.entity.Book;public class JDOMTest {private static ArrayList<Book> booksList = new ArrayList<Book>();/** * @param args */public static void main(String[] args) {// 进行对books.xml文件的JDOM解析// 准备工作// 1.创建一个SAXBuilder的对象SAXBuilder saxBuilder = new SAXBuilder();InputStream in;try {// 2.创建一个输入流,将xml文件加载到输入流中in = new FileInputStream("book.xml");InputStreamReader isr = new InputStreamReader(in, "UTF-8");// 3.通过saxBuilder的build方法,将输入流加载到saxBuilder中Document document = saxBuilder.build(isr);// 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("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).getId());System.out.println(booksList.get(0).getName());}} catch (FileNotFoundException e) {e.printStackTrace();} catch (JDOMException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

三、

0 0
原创粉丝点击