第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

運行結(jié)果出現(xiàn)

源代碼:

package com.imooc.jdotest;

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;


public class JdomTest {
?private? static ArrayList<Book> booksList=new ArrayList<Book>();

?public static void main(String[] args) {
??// TODO Auto-generated method stub
??//1.創(chuàng)建SAXBuilder對象
?
?????? SAXBuilder saxBuilder=new SAXBuilder();
???? //2.創(chuàng)建輸入流,將xml文件加載到輸入流中
???? InputStream in;
?try {
??//in = new FileInputStream("f:\\books.xml");
??in = new FileInputStream("src/res/books.xml");//注意是右斜扛
??//如果出現(xiàn)亂碼問題時,應(yīng)該利用InputStreamReader解決亂碼問題
??InputStreamReader isr=new InputStreamReader(in, "UTF-8");
??//3.通過saxBuilder的build方法,將輸入流加載到saxBuilder中
??Document document=saxBuilder.build(in);
??//4.document對象獲取xml文件的根節(jié)點
???? Element rootElement= document.getRootElement();
???? //5.獲取根節(jié)點下的子節(jié)點的List集合
???? List<Element> bookList=rootElement.getChildren();
???? //繼續(xù)解析
???? for(Element book:bookList){???????
???? ?Book bookEntity=new Book();???????
???? ?System.out.println("開始解析第"+(bookList.indexOf(book)+1)+"本書!");
???? List<Attribute> attribute=book.getAttributes();
???? for(Attribute attr:attribute){
???? ?System.out.println("屬性名:"+attr.getName()+"----屬性值:"+attr.getValue());
???? ?if(attr.getName().equals("id")){
???? ??bookEntity.setID(attr.getValue());
???? ?}
???? }
????? //解析節(jié)點名和節(jié)點值?
???? List<Element> bookChilds= book.getChildren();
???? for(Element child:bookChilds){
???? ?System.out.println("節(jié)點名是:"+child.getName()+"----節(jié)點值是:"+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("結(jié)束解析第"+(bookList.indexOf(book)+1)+"本書!");
???? ?booksList.add(bookEntity);
???????? bookEntity=null;
???????? System.out.println("共有"+booksList.size()+"本書!");
???? ?for(Book book1:booksList){
???? ??System.out.println("------開始------");
???????? System.out.println(book1.getID());
???????? System.out.println(book1.getName());
???????? System.out.println(book1.getAuthor());
???? ? System.out.println(book1.getYear());
???????? System.out.println(book1.getPrice());
???????? System.out.println(book1.getLanguage());
???????? System.out.println("------結(jié)束------");
???? ?}?
???? }
?}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();
?}
? }
}
?
運行結(jié)果:

開始解析第1本書!
屬性名:id----屬性值:1
節(jié)點名是:name----節(jié)點值是:老人與海
節(jié)點名是:author----節(jié)點值是:海明威
節(jié)點名是:year----節(jié)點值是:2015
節(jié)點名是:price----節(jié)點值是:67
結(jié)束解析第1本書!
共有1本書!
------開始------
1
老人與海
海明威
2015
67
null
------結(jié)束------
開始解析第2本書!
屬性名:id----屬性值:2
節(jié)點名是:name----節(jié)點值是:看見
節(jié)點名是:language----節(jié)點值是:Chinses
節(jié)點名是:author----節(jié)點值是:柴靜
節(jié)點名是:year----節(jié)點值是:2014
節(jié)點名是:price----節(jié)點值是:87
結(jié)束解析第2本書!
共有2本書!
------開始------
1
老人與海
海明威
2015
67
null
------結(jié)束------
------開始------
2
看見
柴靜
2014
87
Chinses
------結(jié)束------

正在回答

2 回答

正常,因為有兩個book節(jié)點,節(jié)點解析了兩次(在for循環(huán)中循環(huán)了兩次),而你的booksList的foreach遍歷在這個循環(huán)中,所以遍歷了兩次,而且第一次遍歷booksList集合只有第一本書的內(nèi)容,所以只輸出第一本書的內(nèi)容,第二次遍歷有兩本書的內(nèi)容,所以輸出兩本書的內(nèi)容,這也是為什么運行結(jié)果中生成xml文檔的第一本書生成了兩遍的原因

0 回復(fù) 有任何疑惑可以回復(fù)我~

你的問題是什么?


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕粉3170877 提問者

問題是為什么運行結(jié)果中生成xml文檔的第一本書生成了兩遍?
2016-09-04 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

運行結(jié)果出現(xiàn)

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號