为了账号安全,请及时绑定邮箱和手机立即绑定

我的输出有问题,2,1,null,新手求教

我的输出有问题,2,1,null,新手求教

慕粉3161859 2016-07-08 18:28:47
package com.imooc.jdomtest;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 javax.imageio.stream.FileImageInputStream;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>(); public static void main(String[] args) { // 进行对books.xml文件的JDOM解析 // 准备工作 // 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");// Document document = saxBuilder.build(isr); // 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 attribute : attrList) { // 获取属性名 String attrName = attribute.getName(); // 获取属性值 String attrValue = attribute.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.equals("name")){ bookEntity.setName(child.getValue()); } else if(child.equals("author")){ bookEntity.setAuthor(child.getValue()); } else if(child.equals("year")){ bookEntity.setYear(child.getValue()); } else if(child.equals("price")){ bookEntity.setPrice(child.getValue()); } else if(child.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(); } }}
查看完整描述

1 回答

?
狐狸别跑

TA贡献20条经验 获得超3个赞

为什么不用代码的方式发帖?   眼睛都看花了   大致觉得是这里只id设了值   没有给Name设值 的原因//img1.sycdn.imooc.com//578878010001ea4a05180096.jpg

查看完整回答
1 反对 回复 2016-07-15
  • 1 回答
  • 0 关注
  • 1291 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信