運(yùn)行問(wèn)題。。。
<?xml ?version="1.0” ?encoding=“UTF-8”?>
<bookstore>
? ? ? ? ? <book id="1">
? ? ? ? ? ? ? ? ? ? ?<name>冰與火之歌</name> ? ? ? ?
? ? ? ? ? ? ? ? ? ? ?<auther>喬治馬丁</auther>?
? ? ? ? ? ? ? <year>2014</year>
? ? ? ? ? ? ? <price>88</price>
? ? ? ? ? </book>
? ? ? ? ? <book id="2">
? ? <name>平凡的世界</name>
? ? ?<auther>路遙</auther>
? ? ?<version>版次3.0</version>
? ? ? ? ? ? ? ? ? ? ? <language>中文</language>
? ? ?<price>77</price>
? ? ? ? ? </book>
? ? ? ? ? </bookstore>
package com.imooc.DomTest;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DOMTest {
public static void main(String[] args) {
//創(chuàng)建一個(gè)DocumentbuilderFactory對(duì)象
? ?DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
??
? ?try {
//創(chuàng)建一個(gè)DocumentBuilder對(duì)象
DocumentBuilder db=dbf.newDocumentBuilder();
//通過(guò)DocumentBuilder的parse方法加載book.xml到當(dāng)前項(xiàng)目下
Document document=db.parse("books.xml");
//獲取所有book節(jié)點(diǎn)的集合
NodeList bookList=document.getElementsByTagName("book");
//通過(guò)bookList的getList方法獲取bookList的長(zhǎng)度
System.out.println(bookList.getLength());
//遍歷每一個(gè)book節(jié)點(diǎn)
// for(int i=0;i<2;i++) {
//
// }
}catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
? ? } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
? ? } catch (ParserConfigurationException e) {
? ? ? ? ? ?// TODO Auto-generated catch block
? ? ? ? ? ?e.printStackTrace();
} ? ?
}
}
項(xiàng)目中沒有顯示錯(cuò)誤 ? 運(yùn)行時(shí)是這種情況 ? 編碼方式?jīng)]有問(wèn)題 ?是UTF-8的 ? 謝謝
2018-07-31
你把try。。catch里的e.printstra....方法里的內(nèi)容改寫一下,看看是哪里出了問(wèn)題