java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.setXmlVersion問(wèn)題解決方法
讀取本地的xml文件,通過(guò)DOM進(jìn)行解析,DOM解析的特點(diǎn)就是把整個(gè)xml文件裝載入內(nèi)存中,形成一顆DOM樹(shù)形結(jié)構(gòu),樹(shù)結(jié)構(gòu)是方便遍歷和和操縱。
DOM解析的特性就是讀取xml文件轉(zhuǎn)換為 dom樹(shù)形結(jié)構(gòu),通過(guò)節(jié)點(diǎn)進(jìn)行遍歷。
這是W3c關(guān)于節(jié)點(diǎn)的概念
如果xml中包含有大量的數(shù)據(jù),由于dom一次性把xml裝入內(nèi)存中的特性,所以dom不適合于包含大量數(shù)據(jù)的xml解析。當(dāng)包含有大量xml的時(shí)候,用SAX進(jìn)行解析比較節(jié)省內(nèi)存。
下面是一個(gè)運(yùn)用DOM進(jìn)行解析xml文件的例子:
xml文件結(jié)構(gòu)如下:
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
創(chuàng)建解析xml的類(lèi)如下:
package xml.dom;
import java.io.File;
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 ReadXmlFile {
public static void main(String[] args) {
try{
File xmlFile = new File("src/resource/book.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element: "+doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("book");
for(int i = 0 ; i<nList.getLength();i++){
Node node = nList.item(i);
System.out.println("Node name: "+ node.getNodeName());
Element ele = (Element)node;
System.out.println("----------------------------");
if(node.getNodeType() == Element.ELEMENT_NODE){
System.out.println("book category: "+ ele.getAttribute("category"));
System.out.println("title name: "+ ele.getElementsByTagName("title").item(0).getTextContent());
System.out.println("author name: "+ele.getElementsByTagName("author").item(0).getTextContent());
System.out.println("year :"+ele.getElementsByTagName("year").item(0).getTextContent());
System.out.println("price : "+ele.getElementsByTagName("price").item(0).getTextContent());
System.out.println("-------------------------");
}
}
解析結(jié)果:
Root element: bookstore Node name: book ---------------------------- book category: cooking title name: Everyday Italian author name: Giada De Laurentiis year :2005 price : 30.00 ------------------------- Node name: book ---------------------------- book category: children title name: Harry Potter author name: J K. Rowling year :2005 price : 29.99 ------------------------- Node name: book ---------------------------- book category: web title name: XQuery Kick Start author name: James McGovern year :2003 price : 49.99 ------------------------- Node name: book ---------------------------- book category: web title name: Learning XML author name: Erik T. Ray year :2003 price : 39.95 -------------------------
以上是通過(guò)name獲得對(duì)應(yīng)的值,
下面利用循環(huán)節(jié)點(diǎn)的方式輸出:
循環(huán)節(jié)點(diǎn)輸出方式的代碼如下:
package xml.dom;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ReadXmlFile2 {
public static void main(String[] args) {
try{
File xmlFile = new File("src/resource/book.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element: "+doc.getDocumentElement().getNodeName());
if(doc.hasChildNodes()){
printNode(doc.getChildNodes());
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void printNode(NodeList nodeList){
System.out.println("------------------------");
// System.out.println(nodeList.getLength());
for(int i = 0; i<nodeList.getLength(); i++){
Node node = (Node)nodeList.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE){
System.out.println("node name: "+node.getNodeName());
System.out.println("node value: "+node.getTextContent());
if(node.hasAttributes()){
NamedNodeMap nodeMap = node.getAttributes();
for(int j = 0; j < nodeMap.getLength() ; j++){
Node nodenew = nodeMap.item(j);
System.out.println("node name "+nodenew.getNodeName());
System.out.println("node value "+nodenew.getNodeValue());
}
}
if(node.hasChildNodes()){
printNode(node.getChildNodes());
}
}
}
}
}
輸出結(jié)果如下:
Root element: bookstore ------------------------ node name: bookstore node value: Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick Start James McGovern 2003 49.99 Learning XML Erik T. Ray 2003 39.95 ------------------------ node name: book node value: Everyday Italian Giada De Laurentiis 2005 30.00 node name category node value cooking ------------------------ node name: title node value: Everyday Italian node name lang node value en ------------------------ node name: author node value: Giada De Laurentiis ------------------------ node name: year node value: 2005 ------------------------ node name: price node value: 30.00 ------------------------ node name: book node value: Harry Potter J K. Rowling 2005 29.99 node name category node value children ------------------------ node name: title node value: Harry Potter node name lang node value en ------------------------ node name: author node value: J K. Rowling ------------------------ node name: year node value: 2005 ------------------------ node name: price node value: 29.99 ------------------------ node name: book node value: XQuery Kick Start James McGovern 2003 49.99 node name category node value web ------------------------ node name: title node value: XQuery Kick Start node name lang node value en ------------------------ node name: author node value: James McGovern ------------------------ node name: year node value: 2003 ------------------------ node name: price node value: 49.99 ------------------------ node name: book node value: Learning XML Erik T. Ray 2003 39.95 node name category node value web node name cover node value paperback ------------------------ node name: title node value: Learning XML node name lang node value en ------------------------ node name: author node value: Erik T. Ray ------------------------ node name: year node value: 2003 ------------------------ node name: price node value: 39.95 ------------------------
關(guān)于節(jié)點(diǎn)的問(wèn)題:
<book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book>
對(duì)于 book應(yīng)用:doc.getChildNodes() 得到一個(gè)NodeList其中NodeList的長(zhǎng)度為9
9個(gè)節(jié)點(diǎn)分別如下:
title節(jié)點(diǎn)
lang節(jié)點(diǎn)
Everyday節(jié)點(diǎn)
author節(jié)點(diǎn)
Giada De Laurentiis節(jié)點(diǎn)
year節(jié)點(diǎn)
2005節(jié)點(diǎn)
price節(jié)點(diǎn)
30.00節(jié)點(diǎn)
- Java AbstractMethodError原因案例詳解
- 詳解Matisse與Glide--java.lang.NoSuchMethodError:com.bumptech.glide.RequestManager.load
- Java異常 Factory method''sqlSessionFactory''rew exception;ested exception is java.lang.NoSuchMethodError:
- 解決啟動(dòng)Azkaban報(bào)錯(cuò)問(wèn)題:java.lang.NoSuchMethodError: com.google.common.collect.ImmutableMap.toImmutableMap
- 解決 java.lang.NoSuchMethodError的錯(cuò)誤
- Java AbstractMethodError案例分析詳解
相關(guān)文章
詳解Java如何實(shí)現(xiàn)基于Redis的分布式鎖
在不同進(jìn)程需要互斥地訪(fǎng)問(wèn)共享資源時(shí),分布式鎖是一種非常有用的技術(shù)手段。這篇文章運(yùn)用圖文和實(shí)例代碼介紹了Java如何實(shí)現(xiàn)基于Redis的分布式鎖,文章介紹的很詳細(xì),對(duì)Java和Redis剛興趣的朋友們可以參考借鑒,下面來(lái)一起看看。2016-08-08
使用RestTemplate調(diào)用https接口跳過(guò)證書(shū)驗(yàn)證
這篇文章主要介紹了使用RestTemplate調(diào)用https接口跳過(guò)證書(shū)驗(yàn)證,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java導(dǎo)出網(wǎng)頁(yè)表格Excel過(guò)程詳解
這篇文章主要介紹了Java導(dǎo)出網(wǎng)頁(yè)表格Excel過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
mybatis-plus自定義排序的實(shí)現(xiàn)
本文主要介紹了mybatis-plus自定義排序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Spring源碼剖析之Spring處理循環(huán)依賴(lài)的問(wèn)題
大家都知道循環(huán)依賴(lài)依賴(lài)指的是Bean與Bean之間的依賴(lài)關(guān)系,循環(huán)依賴(lài)指的是兩個(gè)或者多個(gè)Bean相互依賴(lài),本文通過(guò)代碼示例給大家講解Spring處理循環(huán)依賴(lài)的問(wèn)題,感興趣的朋友一起看看吧2021-06-06
java springboot poi 從controller 接收不同類(lèi)型excel 文件處理
這篇文章主要介紹了java springboot poi 從controller 接收不同類(lèi)型excel 文件處理,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
2種Java刪除ArrayList中的重復(fù)元素的方法
這篇文章主要介紹了2種Java刪除ArrayList中的重復(fù)元素的方法,感興趣的朋友可以參考下2015-08-08

