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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Node ge= gen.item(i);編譯及運(yùn)行錯(cuò)誤。求大神指教。

import?java.io.IOException;
import?java.util.Collection;

import?javax.xml.parsers.DocumentBuilder;
import?javax.xml.parsers.DocumentBuilderFactory;
import?javax.xml.parsers.ParserConfigurationException;
import?javax.xml.soap.Node;

import?org.w3c.dom.Document;
import?org.w3c.dom.NamedNodeMap;
import?org.w3c.dom.NodeList;
import?org.xml.sax.SAXException;


public?class?DomTest?{

	public?static?void?main(String[]?args)?throws?ParserConfigurationException,?SAXException,?IOException?{
		//?TODO?Auto-generated?method?stub
//????錯(cuò)誤:調(diào)用前沒有new???DocumentBuilderFactory?dbf=new?DocumentBuilderFactory.newInstance();?
		DocumentBuilderFactory?dbf=?DocumentBuilderFactory.newInstance();?//創(chuàng)建xml解析器,用newInstance方法創(chuàng)建實(shí)例
		DocumentBuilder?db=dbf.newDocumentBuilder();//定義使可以從xml中獲得document實(shí)例
		Document?d=?db.parse("haha.xml");//解析的放入document中,用w3.。。
		NodeList?gen=d.getElementsByTagName("book");
		System.out.println("一共有"+gen.getLength()+"本書");
		for(int?i=0;i<gen.getLength();i++){
			
			System.out.println(gen.item(i));
//			item為節(jié)點(diǎn)的元素。getAttributes();是得到該節(jié)點(diǎn)的屬性集合
			Node?ge=(Node)?gen.item(i);	//運(yùn)行異常,和老師寫的不一樣,老師寫的是	Node?ge=?gen.item(i);這樣寫編譯器會(huì)報(bào)錯(cuò)。
			NamedNodeMap?att=ge.getAttributes();
			for(int?j=0;j<att.getLength();j++){
				Node?g=(Node)att.item(j);
				System.out.println(g);
				
			}
		}
		
		
	}

}

錯(cuò)誤提示:

一共有2本書

Exception in thread "main" [book: null]

java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredElementImpl cannot be cast to javax.xml.soap.Node

at xmlTest.DomTest.main(DomTest.java:32)


正在回答

1 回答

你這個(gè)是導(dǎo)入的包不對(duì),import org.w3c.dom.Node;應(yīng)該是這個(gè),而不是你這個(gè)程序中的那個(gè)

import?javax.xml.soap.Node;
1 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

Node ge= gen.item(i);編譯及運(yùn)行錯(cuò)誤。求大神指教。

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

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

幫助反饋 APP下載

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

公眾號(hào)

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