Java:DocumentBuilderFactory調(diào)用XML的方法實(shí)例
首先得到:得到 DOM 解析器的工廠實(shí)例 DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
然后從 DOM 工廠獲得 DOM 解析器
DocumentBuilder dombuilder=domfac.newDocumentBuilder();
)把要解析的 XML 文檔轉(zhuǎn)化為輸入流,以便 DOM 解析器解析它
InputStream is= new FileInputStream("test1.xml");
( 4 )解析 XML 文檔的輸入流,得到一個(gè) Document
Document doc=dombuilder.parse(is);
( 5 )得到 XML 文檔的根節(jié)點(diǎn)
Element root=doc.getDocumentElement();
( 6 )得到節(jié)點(diǎn)的子節(jié)點(diǎn)
NodeList books=root.getChildNodes();
package com.st.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlReader {
public static void main(String[] args) {
XmlReader reader = new XmlReader();
}
public XmlReader(){
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder domBuilder = domfac.newDocumentBuilder();
InputStream is = new FileInputStream(new File("D:/test1.xml"));
Document doc = domBuilder.parse(is);
Element root = doc.getDocumentElement();
NodeList books = root.getChildNodes();
if(books!=null){
for (int i = 0; i < books.getLength(); i++) {
Node book = books.item(i);
if(book.getNodeType()==Node.ELEMENT_NODE) {
//(7)取得節(jié)點(diǎn)的屬性值
String email=book.getAttributes().getNamedItem("email").getNodeValue();
System.out.println(email);
//注意,節(jié)點(diǎn)的屬性也是它的子節(jié)點(diǎn)。它的節(jié)點(diǎn)類(lèi)型也是Node.ELEMENT_NODE
//(8)輪循子節(jié)點(diǎn)
for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()) {
if(node.getNodeType()==Node.ELEMENT_NODE) {
if(node.getNodeName().equals("name")) {
String name=node.getNodeValue();
String name1=node.getFirstChild().getNodeValue();
System.out.println(name);
System.out.println(name1);
}
if(node.getNodeName().equals("price")) {
String price=node.getFirstChild().getNodeValue();
System.out.println(price);
}
}
}
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相關(guān)文章
啟動(dòng)springboot應(yīng)用因未配置數(shù)據(jù)庫(kù)報(bào)錯(cuò)的解決方案
這篇文章主要介紹了啟動(dòng)springboot應(yīng)用因未配置數(shù)據(jù)庫(kù)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java實(shí)現(xiàn)拖拽文件上傳dropzone.js的簡(jiǎn)單使用示例代碼
本篇文章主要介紹了Java實(shí)現(xiàn)拖拽文件上傳dropzone.js的簡(jiǎn)單使用示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
基于Bigdecimal科學(xué)計(jì)數(shù)問(wèn)題
這篇文章主要介紹了基于Bigdecimal科學(xué)計(jì)數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)動(dòng)態(tài)權(quán)限問(wèn)題
這篇文章主要介紹了SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
JPA?使用criteria簡(jiǎn)單查詢(xún)工具類(lèi)方式
這篇文章主要介紹了JPA?使用criteria簡(jiǎn)單查詢(xún)工具類(lèi)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java groovy內(nèi)存回收測(cè)試步驟解析
這篇文章主要介紹了Java groovy內(nèi)存回收測(cè)試步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
基于SpringBoot+vue實(shí)現(xiàn)前后端數(shù)據(jù)加解密
這篇文章主要給大家介紹了基于SpringBoot+vue實(shí)現(xiàn)前后端數(shù)據(jù)加解密,文中有詳細(xì)的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴可以自己動(dòng)手試一試2023-08-08
詳解Java的JDBC API的存儲(chǔ)過(guò)程與SQL轉(zhuǎn)義語(yǔ)法的使用
這篇文章主要介紹了詳解Java的JDBC API的存儲(chǔ)過(guò)程與SQL轉(zhuǎn)義語(yǔ)法的使用,JDBC是Java用于連接使用各種數(shù)據(jù)庫(kù)的API,需要的朋友可以參考下2015-12-12

