課程
/后端開發(fā)
/Java
/Java眼中的XML---文件讀取
有把老師最后的那個(gè)聯(lián)系做了的嗎?
2015-09-15
源自:Java眼中的XML---文件讀取 2-3
正在回答
在3-4 中你會(huì)看到用SAX將XML文件存儲在Book類中......那個(gè)比較容易理解
網(wǎng)絡(luò)小曦 提問者
前提是我們不知道xml中的那些內(nèi)容啊,要是所有屬性都知道了,那叫我們解析xml文件的內(nèi)容就沒有意義了
//勉強(qiáng)寫出來了,大家湊合看
package com.lianxi.baochushujujiegou;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TextDOMXMLDemo {
public static void main(String[] args) {
book book1 = new book();
book book2 = new book();
//從 XML 文檔獲取生成 DOM 對象樹的解析器
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//newInstance()是一個(gè)靜態(tài)方法,所以可以不創(chuàng)建對象就可以直接只用
try {
//使用當(dāng)前配置的參數(shù)創(chuàng)建一個(gè)新的 DocumentBuilder 實(shí)例。
DocumentBuilder db = dbf.newDocumentBuilder();
/*
* DocumentBuilder的定義:定義 API, 使其從 XML 文檔獲取 DOM 文檔實(shí)例。使用此類,
* 應(yīng)用程序員可以從 XML 獲取一個(gè) Document。
此類的實(shí)例可以從 DocumentBuilderFactory.newDocumentBuilder() 方法獲取。
獲取此類的實(shí)例之后,將可以從各種輸入源解析 XML。
這些輸入源有 InputStreams、Files、URL 和 SAX InputSources。
* */
//創(chuàng)建i,j,k,作為循環(huán)的變量,為了后邊方便保存到book的對象中
int i = 0;
int j = 0;
int k = 0;
int flag = 0;//判斷正在讀第幾本書
//創(chuàng)建一個(gè)XML文件的根(Document 接口表示整個(gè) HTML 或 XML 文檔。從概念上講,它是文檔樹的根,并提供對文檔數(shù)據(jù)的基本訪問。)
Document docunment = db.parse("D:\\javaWorkHome\\XML(UTF-8)\\src\\books.xml");
//獲得標(biāo)簽名為book的節(jié)點(diǎn)的集合,并打印
NodeList bookList = docunment.getElementsByTagName("book");
System.out.println("獲取到了"+bookList.getLength()+"本書");
//遍歷節(jié)點(diǎn),開始訪問節(jié)點(diǎn)內(nèi)的元素
for(i=0; i<bookList.getLength(); i++,flag++) {
System.out.println("下面開始遍歷第"+ (i+1) + "本書");
System.out.println("============遍歷開始============");
//獲取名為book的單個(gè)節(jié)點(diǎn)
Node book = bookList.item(i);
//獲取該節(jié)點(diǎn)的所有屬性圖 ? 屬性指的是標(biāo)簽<>中的內(nèi)容,是對標(biāo)簽的說明解釋
NamedNodeMap attrs = book.getAttributes();
//遍歷book的屬性
System.out.println("第"+(i+1)+"本書共有"+ attrs.getLength()+ "屬性");
for(j=0; j<attrs.getLength(); j++) {
//獲取book節(jié)點(diǎn)上的某一個(gè)屬性
Node attr = attrs.item(j);
//輸出屬性名和屬性值
System.out.print("屬性名為"+ attr.getNodeName());
System.out.println(",屬性值為"+ attr.getNodeValue());
}
//解析book節(jié)點(diǎn)的子節(jié)點(diǎn)
NodeList childNodes = book.getChildNodes();
System.out.println("第"+(i+1)+"本書共有"+childNodes.getLength()
+"個(gè)子節(jié)點(diǎn)");//子節(jié)點(diǎn)包括<>標(biāo)簽和標(biāo)簽之間的空白和換行
for(k=0; k<childNodes.getLength(); k++) {
//區(qū)分出text類型的node以及element類型node
if(childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
System.out.print("第"+k+"個(gè)元素,"+childNodes.item(k).getNodeName()+":");
System.out.println(childNodes.item(k).getFirstChild().getNodeValue());
System.out.println("============遍歷結(jié)束============");
//將從XML中讀取的文件放在對象中
switch (flag) {
case 0:
book1.setName(childNodes.item(1).getFirstChild().getNodeValue());
book1.setAuthor(childNodes.item(3).getFirstChild().getNodeValue());
book1.setYear(Integer.parseInt(childNodes.item(5).getFirstChild().getNodeValue()) );
book1.setPrice(Double.parseDouble(childNodes.item(7).getFirstChild().getNodeValue()));
break;
case 1:
book2.setName(childNodes.item(1).getFirstChild().getNodeValue());
book2.setLanguage(childNodes.item(7).getFirstChild().getNodeValue());
book2.setYear(Integer.parseInt(childNodes.item(3).getFirstChild().getNodeValue()) );
book2.setPrice(Double.parseDouble(childNodes.item(5).getFirstChild().getNodeValue()));
} catch (SAXException e) {
// TODO 自動(dòng)生成的 catch 塊
e.printStackTrace();
} catch (IOException e) {
} catch (ParserConfigurationException e) {
}//newDocumentBuilder()是一個(gè)抽象方法
System.out.println(book1.getYear());
System.out.println(book1.getAuthor());
System.out.println(book1.getLanguage());
System.out.println(book1.getPrice());
System.out.println(book2.getYear());
System.out.println(book2.getAuthor());
System.out.println(book2.getLanguage());
System.out.println(book2.getPrice());
創(chuàng)建book類,應(yīng)該包含XML文件中所有的屬性,然后每讀到一本書的時(shí)候就創(chuàng)建一個(gè)book對象,并將其中的屬性保存對相對應(yīng)的對象屬性中。然后將book對象最為集合保存起來。
CyberLiu
舉報(bào)
通過Java認(rèn)識并且創(chuàng)造XML文件,如何應(yīng)用 Java“解析 XML
3 回答為什么我跟著老師做下來,在endDocument方法后面重寫那個(gè)方法,最后輸出無法輸出book的屬性值,然后把把輸出book屬性值的代碼放在第一個(gè)startElement方法中才可以輸出book屬性值
1 回答最后的DOM4J ?
2 回答我想問問老師把鼠標(biāo)放上面就會(huì)彈出返回的數(shù)據(jù)類型那個(gè)操作是什么怎么做到的?
1 回答怎樣把兩個(gè)xml文件都在一個(gè)html網(wǎng)頁上顯示出來呢?我寫的只會(huì)執(zhí)行最后一個(gè)。
5 回答最后的遍歷booksList怎么遍歷
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2015-09-16
在3-4 中你會(huì)看到用SAX將XML文件存儲在Book類中......那個(gè)比較容易理解
2016-04-20
前提是我們不知道xml中的那些內(nèi)容啊,要是所有屬性都知道了,那叫我們解析xml文件的內(nèi)容就沒有意義了
2015-09-17
//勉強(qiáng)寫出來了,大家湊合看
package com.lianxi.baochushujujiegou;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TextDOMXMLDemo {
public static void main(String[] args) {
book book1 = new book();
book book2 = new book();
//從 XML 文檔獲取生成 DOM 對象樹的解析器
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//newInstance()是一個(gè)靜態(tài)方法,所以可以不創(chuàng)建對象就可以直接只用
try {
//使用當(dāng)前配置的參數(shù)創(chuàng)建一個(gè)新的 DocumentBuilder 實(shí)例。
DocumentBuilder db = dbf.newDocumentBuilder();
/*
* DocumentBuilder的定義:定義 API, 使其從 XML 文檔獲取 DOM 文檔實(shí)例。使用此類,
* 應(yīng)用程序員可以從 XML 獲取一個(gè) Document。
此類的實(shí)例可以從 DocumentBuilderFactory.newDocumentBuilder() 方法獲取。
獲取此類的實(shí)例之后,將可以從各種輸入源解析 XML。
這些輸入源有 InputStreams、Files、URL 和 SAX InputSources。
* */
try {
//創(chuàng)建i,j,k,作為循環(huán)的變量,為了后邊方便保存到book的對象中
int i = 0;
int j = 0;
int k = 0;
int flag = 0;//判斷正在讀第幾本書
//創(chuàng)建一個(gè)XML文件的根(Document 接口表示整個(gè) HTML 或 XML 文檔。從概念上講,它是文檔樹的根,并提供對文檔數(shù)據(jù)的基本訪問。)
Document docunment = db.parse("D:\\javaWorkHome\\XML(UTF-8)\\src\\books.xml");
//獲得標(biāo)簽名為book的節(jié)點(diǎn)的集合,并打印
NodeList bookList = docunment.getElementsByTagName("book");
System.out.println("獲取到了"+bookList.getLength()+"本書");
//遍歷節(jié)點(diǎn),開始訪問節(jié)點(diǎn)內(nèi)的元素
for(i=0; i<bookList.getLength(); i++,flag++) {
System.out.println("下面開始遍歷第"+ (i+1) + "本書");
System.out.println("============遍歷開始============");
//獲取名為book的單個(gè)節(jié)點(diǎn)
Node book = bookList.item(i);
//獲取該節(jié)點(diǎn)的所有屬性圖 ? 屬性指的是標(biāo)簽<>中的內(nèi)容,是對標(biāo)簽的說明解釋
NamedNodeMap attrs = book.getAttributes();
//遍歷book的屬性
System.out.println("第"+(i+1)+"本書共有"+ attrs.getLength()+ "屬性");
for(j=0; j<attrs.getLength(); j++) {
//獲取book節(jié)點(diǎn)上的某一個(gè)屬性
Node attr = attrs.item(j);
//輸出屬性名和屬性值
System.out.print("屬性名為"+ attr.getNodeName());
System.out.println(",屬性值為"+ attr.getNodeValue());
}
//解析book節(jié)點(diǎn)的子節(jié)點(diǎn)
NodeList childNodes = book.getChildNodes();
System.out.println("第"+(i+1)+"本書共有"+childNodes.getLength()
+"個(gè)子節(jié)點(diǎn)");//子節(jié)點(diǎn)包括<>標(biāo)簽和標(biāo)簽之間的空白和換行
for(k=0; k<childNodes.getLength(); k++) {
//區(qū)分出text類型的node以及element類型node
if(childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
System.out.print("第"+k+"個(gè)元素,"+childNodes.item(k).getNodeName()+":");
System.out.println(childNodes.item(k).getFirstChild().getNodeValue());
}
}
System.out.println("============遍歷結(jié)束============");
//將從XML中讀取的文件放在對象中
switch (flag) {
case 0:
book1.setName(childNodes.item(1).getFirstChild().getNodeValue());
book1.setAuthor(childNodes.item(3).getFirstChild().getNodeValue());
book1.setYear(Integer.parseInt(childNodes.item(5).getFirstChild().getNodeValue()) );
book1.setPrice(Double.parseDouble(childNodes.item(7).getFirstChild().getNodeValue()));
break;
case 1:
book2.setName(childNodes.item(1).getFirstChild().getNodeValue());
book2.setLanguage(childNodes.item(7).getFirstChild().getNodeValue());
book2.setYear(Integer.parseInt(childNodes.item(3).getFirstChild().getNodeValue()) );
book2.setPrice(Double.parseDouble(childNodes.item(5).getFirstChild().getNodeValue()));
break;
}
}
} catch (SAXException e) {
// TODO 自動(dòng)生成的 catch 塊
e.printStackTrace();
} catch (IOException e) {
// TODO 自動(dòng)生成的 catch 塊
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO 自動(dòng)生成的 catch 塊
e.printStackTrace();
}//newDocumentBuilder()是一個(gè)抽象方法
System.out.println(book1.getYear());
System.out.println(book1.getAuthor());
System.out.println(book1.getLanguage());
System.out.println(book1.getPrice());
System.out.println(book1.getYear());
System.out.println(book2.getYear());
System.out.println(book2.getAuthor());
System.out.println(book2.getLanguage());
System.out.println(book2.getPrice());
System.out.println(book2.getYear());
}
}
2015-09-15
創(chuàng)建book類,應(yīng)該包含XML文件中所有的屬性,然后每讀到一本書的時(shí)候就創(chuàng)建一個(gè)book對象,并將其中的屬性保存對相對應(yīng)的對象屬性中。然后將book對象最為集合保存起來。