java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.setXmlVersion問題解決方法
讀取本地的xml文件,通過DOM進(jìn)行解析,DOM解析的特點(diǎn)就是把整個(gè)xml文件裝載入內(nèi)存中,形成一顆DOM樹形結(jié)構(gòu),樹結(jié)構(gòu)是方便遍歷和和操縱。
DOM解析的特性就是讀取xml文件轉(zhuǎn)換為 dom樹形結(jié)構(gòu),通過節(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的類如下:
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 -------------------------
以上是通過name獲得對應(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)的問題:
<book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book>
對于 book應(yīng)用:doc.getChildNodes() 得到一個(gè)NodeList其中NodeList的長度為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:
- 解決啟動Azkaban報(bào)錯問題:java.lang.NoSuchMethodError: com.google.common.collect.ImmutableMap.toImmutableMap
- 解決 java.lang.NoSuchMethodError的錯誤
- Java AbstractMethodError案例分析詳解
相關(guān)文章
詳解Java如何實(shí)現(xiàn)基于Redis的分布式鎖
在不同進(jìn)程需要互斥地訪問共享資源時(shí),分布式鎖是一種非常有用的技術(shù)手段。這篇文章運(yùn)用圖文和實(shí)例代碼介紹了Java如何實(shí)現(xiàn)基于Redis的分布式鎖,文章介紹的很詳細(xì),對Java和Redis剛興趣的朋友們可以參考借鑒,下面來一起看看。2016-08-08使用RestTemplate調(diào)用https接口跳過證書驗(yàn)證
這篇文章主要介紹了使用RestTemplate調(diào)用https接口跳過證書驗(yàn)證,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Java導(dǎo)出網(wǎng)頁表格Excel過程詳解
這篇文章主要介紹了Java導(dǎo)出網(wǎng)頁表格Excel過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07mybatis-plus自定義排序的實(shí)現(xiàn)
本文主要介紹了mybatis-plus自定義排序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Spring源碼剖析之Spring處理循環(huán)依賴的問題
大家都知道循環(huán)依賴依賴指的是Bean與Bean之間的依賴關(guān)系,循環(huán)依賴指的是兩個(gè)或者多個(gè)Bean相互依賴,本文通過代碼示例給大家講解Spring處理循環(huán)依賴的問題,感興趣的朋友一起看看吧2021-06-06java springboot poi 從controller 接收不同類型excel 文件處理
這篇文章主要介紹了java springboot poi 從controller 接收不同類型excel 文件處理,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-102種Java刪除ArrayList中的重復(fù)元素的方法
這篇文章主要介紹了2種Java刪除ArrayList中的重復(fù)元素的方法,感興趣的朋友可以參考下2015-08-08