java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.setXmlVersion問題解決方法
讀取本地的xml文件,通過DOM進(jìn)行解析,DOM解析的特點就是把整個xml文件裝載入內(nèi)存中,形成一顆DOM樹形結(jié)構(gòu),樹結(jié)構(gòu)是方便遍歷和和操縱。
DOM解析的特性就是讀取xml文件轉(zhuǎn)換為 dom樹形結(jié)構(gòu),通過節(jié)點進(jìn)行遍歷。
這是W3c關(guān)于節(jié)點的概念
如果xml中包含有大量的數(shù)據(jù),由于dom一次性把xml裝入內(nèi)存中的特性,所以dom不適合于包含大量數(shù)據(jù)的xml解析。當(dāng)包含有大量xml的時候,用SAX進(jìn)行解析比較節(jié)省內(nèi)存。
下面是一個運用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é)點的方式輸出:
循環(huán)節(jié)點輸出方式的代碼如下:
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é)點的問題:
<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() 得到一個NodeList其中NodeList的長度為9
9個節(jié)點分別如下:
title節(jié)點
lang節(jié)點
Everyday節(jié)點
author節(jié)點
Giada De Laurentiis節(jié)點
year節(jié)點
2005節(jié)點
price節(jié)點
30.00節(jié)點
- 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報錯問題:java.lang.NoSuchMethodError: com.google.common.collect.ImmutableMap.toImmutableMap
- 解決 java.lang.NoSuchMethodError的錯誤
- Java AbstractMethodError案例分析詳解
相關(guān)文章
ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型方法步驟
Elasticsearch存儲數(shù)據(jù)之前需要先創(chuàng)建索引,類似于結(jié)構(gòu)型數(shù)據(jù)庫建庫建表,創(chuàng)建索引時定義了每個字段的索引方式和數(shù)據(jù)類型,這篇文章主要給大家介紹了關(guān)于ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型的方法步驟,需要的朋友可以參考下2023-09-09
MyBatis實現(xiàn)插入大量數(shù)據(jù)方法詳解
最近在公司項目開發(fā)中遇到批量數(shù)據(jù)插入或者更新,下面這篇文章主要給大家介紹了關(guān)于MyBatis實現(xiàn)批量插入的相關(guān)資料,需要的朋友可以參考下2022-11-11
maven 在執(zhí)行package,install,deploy時使用clean與不使用clean的不同之處
有時候用mvn install后,新改的內(nèi)容不生效,一定要后來使用mvn clean install 才生效,由于之前沒有做記錄,以及記不清是什么情況下才會出現(xiàn)的問題,于是想看看clean和不clean的區(qū)別,感興趣的朋友跟隨小編一起看看吧2021-08-08
Spring+SpringMVC+MyBatis整合詳細(xì)教程(SSM)
Spring是一個開源框架,Spring是于2003 年興起的一個輕量級的Java 開發(fā)框架。這篇文章主要介紹了Spring+SpringMVC+MyBatis整合詳細(xì)教程(SSM),需要的朋友可以參考下2017-10-10

