dom4kj解析xml
生活随笔
收集整理的這篇文章主要介紹了
dom4kj解析xml
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
xml解析你們都清不清楚,你們之前都學(xué)過沒有,我記得是剛開始學(xué)JAVA的時候,可能都講過XML的解析的,XML解析清不清楚,外面有很多關(guān)于XML解析的框架,在這里我就講一下DOM4J怎么解析的,我們講一下XML怎么做解析,我們這里有一個xml,有一個根節(jié)點(diǎn),有student1,有student2,是不是這樣的,我們之前的Spring的配置文件,他其實(shí)也是這樣的,id叫做屬性,還有屬性的value,待會我給你們大體演示一下,你們把全部的依賴都依賴一下,maven相關(guān)的依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><!-- 引入Spring-AOP等相關(guān)Jar --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.6.1</version></dependency><dependency><groupId>aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.5.3</version></dependency><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.1_2</version></dependency><!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.37</version></dependency><!-- https://mvnrepository.com/artifact/dom4j/dom4j --><dependency><groupId>dom4j</groupId><artifactId>dom4j</artifactId><version>1.6.1</version></dependency><!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency></dependencies></project>
<?xml version="1.0" encoding="UTF-8"?>
<students><student1 id="001"><微信公眾號>每特學(xué)院</微信公眾號><學(xué)號>20140101</學(xué)號><地址>北京海淀區(qū)</地址><座右銘>要么強(qiáng)大,要么聽話</座右銘></student1><student2 id="002"><新浪微博>螞蟻課堂</新浪微博><學(xué)號>20140102</學(xué)號><地址>北京朝陽區(qū)</地址><座右銘>在哭泣中學(xué)會堅(jiān)強(qiáng)</座右銘></student2>
</students>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 讀取到節(jié)點(diǎn)的屬性,獲取到id的屬性,拿到一個service之后,我再獲取class地址,然后獲取到class地址之后,利用反射機(jī)制去獲取對象,這個過程就是這樣的--><bean id="userService" class="com.learn.service.impl.UserServiceImpl">
</beans>
package com.learn;import java.io.InputStream;
import java.util.Iterator;
import java.util.List;import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;/*** 首先獲取當(dāng)前節(jié)點(diǎn)的名稱* 是不是先拿到students的根節(jié)點(diǎn)* 然后內(nèi)容是為空的* 然后獲取他的子節(jié)點(diǎn)* 然后獲取到子節(jié)點(diǎn)上的屬性* 然后屬性有這樣的一個id* 然后屬性的內(nèi)容為多少* 是不是001* 看到?jīng)]有* 所以大家記住這個方法* 獲取節(jié)點(diǎn)上的屬性* attribute.getText()* 這個是獲取屬性的內(nèi)容* 馬上我們就要用到這個API了* 其實(shí)Spring里面也是這么寫的* API我就不去細(xì)說了* 反正大家要記住這個API* 獲得屬性* 就可以獲得傳入的值* * 怎么解析XML比較簡單* 你只要記得API獲取當(dāng)前節(jié)點(diǎn)的屬性* XML與JSON的區(qū)別* XML一般是屬于重量級的* 占用寬帶比較大的* JSON是輕量級的* Rest進(jìn)行請求方式的* 因?yàn)镴SON格式是輕量級的* 而且定義起來也比較簡單* 我在互聯(lián)網(wǎng)項(xiàng)目開發(fā)的時候* 很少用到這樣一個XML* 都是JSON* 除了和銀行對接會用到XML之外* 基本上都是用JSON* 所以給大家說一下* 一定要把JSON學(xué)好* 怎么去解析JSON* 因?yàn)镴SON在企業(yè)中也是用的比較多的* 還有XML怎么解析* 基本的幾個API* 報(bào)文格式也有XML的* 我覺得XML格式真的特別少* 都是用JSON格式的* 這個過程我就不去細(xì)說了* 一般互聯(lián)網(wǎng)項(xiàng)目都是用JSON作通信的* Restful風(fēng)格* webservice底層遵循SOAP* XML報(bào)文傳輸?shù)? 所以可能還是有很大的區(qū)別的* 你們肯定用的是webservice* 互聯(lián)網(wǎng)項(xiàng)目我是沒見過用xml方式的* 都是用JSON格式的* * * * @author Leon.Sun**/
public class XmlUtils {public static void main(String[] args) throws Exception {XmlUtils xmlUtils = new XmlUtils();xmlUtils.test001();}public void test001() throws Exception {SAXReader saxReader = new SAXReader();/*** 你可以寫完整的路徑地址* 我不建議寫類的完整路徑地址* 怎么做呢* 你直接把student.xml文件copy到目錄里面去* 怎么獲取上下文路徑* 獲得我的項(xiàng)目的上下文路徑* 怎么獲取* 你們不要去讀F盤的xml* 怎么做呢* 在這里我要講個方法* 叫做什么方法呢* 這個方法* * 這里只是讀取一個xml文件* 讀取完了之后我們在第二步怎么去做呢* */Document read = saxReader.read(getClassPath("student.xml"));// 獲取根節(jié)點(diǎn)/*** 讀取根節(jié)點(diǎn)* 再讀取到子節(jié)點(diǎn)* 通過遞歸算法不停的去調(diào)用* 讀取當(dāng)前節(jié)點(diǎn)的下個節(jié)點(diǎn)的一個信息* 在這里我們可以拿到他的根節(jié)點(diǎn)* 然后拿到Document對象* 然后再從根節(jié)點(diǎn)開始讀* 拿到root節(jié)點(diǎn)之后* 我們要寫一個方法來讀取下面的子節(jié)點(diǎn)* */Element rootElement = read.getRootElement();getNodes(rootElement);}public InputStream getClassPath(String xmlPath) {/*** getClass().getClassLoader()他可以獲取你上下文路徑* 最好你把它封裝一下* 我們拿到一個InputStream* 把xmlPath這個傳進(jìn)去* 就得到一個InputStream流* 拿到之后我們就直接返回出來* */InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(xmlPath);return resourceAsStream;}/*** 這里就是用遞歸的不停的往下個節(jié)點(diǎn)讀取* 像是二叉樹結(jié)構(gòu)一樣的* 傳入一個Element對象* 這里我們怎么去進(jìn)行做* * * @param rootElement*/public static void getNodes(Element rootElement) {/*** 先獲取到節(jié)點(diǎn)名稱* rootElement.getName()* 獲取當(dāng)前節(jié)點(diǎn)的名稱* 獲取完了以后* 下面還有很多屬性方法* */System.out.println("獲取當(dāng)前名稱:" + rootElement.getName());// 獲取屬性信息/*** 獲取屬性方法* 里面怎么去寫呢* 這里是屬性* 最好是用泛型接收* * */List<Attribute> attributes = rootElement.attributes();/*** 去遍歷一下* 這個屬性有什么信息呢* * */for (Attribute attribute : attributes) {/*** 這里面有幾個方法* 第一個方法是getName* 表示屬性的名稱* 然后還有個getText* 獲取別人的內(nèi)容* 待會會細(xì)講幾個API的* 先去手寫一下這幾個API* 獲取完屬性之后呢* 獲取屬性的value* * */System.out.println("屬性:" + attribute.getName() + "---" + attribute.getText());}/*** 獲取屬性的value* 去空格然后拿到一下* */String value = rootElement.getTextTrim();if (!StringUtils.isEmpty(value)) {System.out.println("value:" + value);}// 使用迭代器遍歷,繼續(xù)遍歷子節(jié)點(diǎn)/*** 你要遞歸往下個節(jié)點(diǎn)讀取的話* 就會用到一個遞歸算法* 這里最好是加上一個泛型Element* 遍歷的時候是可以獲取到一個類型的* * */Iterator<Element> elementIterator = rootElement.elementIterator();/*** 使用迭代器* 獲取子節(jié)點(diǎn)的信息* 拿到迭代器后進(jìn)行while循環(huán)* 相當(dāng)于迭代器里有下個節(jié)點(diǎn)* 如果有下個節(jié)點(diǎn)就讓他繼續(xù)的* */while (elementIterator.hasNext()) {Element next = elementIterator.next();/*** 這里不要傳root* 而是要傳當(dāng)前的這個節(jié)點(diǎn)* * */getNodes(next);}}
}
什么是XML
它是可擴(kuò)展標(biāo)記語言(Extensible Markup Language,簡稱XML),是一種標(biāo)記語言。
XML 全稱為可擴(kuò)展的標(biāo)記語言。主要用于描述數(shù)據(jù)和用作配置文件。
XML 文檔在邏輯上主要由一下 5 個部分組成:
XML 聲明:指明所用 XML 的版本、文檔的編碼、文檔的獨(dú)立性信息
文檔類型聲明:指出 XML 文檔所用的 DTD
元素:由開始標(biāo)簽、元素內(nèi)容和結(jié)束標(biāo)簽構(gòu)成
注釋:以結(jié)束,用于對文檔中的內(nèi)容起一個說明作用
處理指令:通過處理指令來通知其他應(yīng)用程序來處理非 XML 格式的數(shù)據(jù),格式為
XML 文檔的根元素被稱為文檔元素,它和在其外部出現(xiàn)的處理指令、注釋等作為文檔實(shí)體的子節(jié)點(diǎn),
根元素本身和其內(nèi)部的子元素也是一棵樹。
Dom4j與Sax區(qū)別dom4j不適合大文件的解析,因?yàn)樗且幌伦訉⑽募虞d到內(nèi)存中,所以有可能出現(xiàn)內(nèi)存溢出,sax是基于事件來對xml進(jìn)行解析的,所以他可以解析大文件的xml,也正是因?yàn)槿绱?#xff0c;所以dom4j可以對xml進(jìn)行靈活的增刪改查和導(dǎo)航,而sax沒有這么強(qiáng)的靈活性,所以sax經(jīng)常是用來解析大型xml文件,而要對xml文件進(jìn)行一些靈活(crud)操作就用dom4j。
XML與JSON區(qū)別
Xml是重量級數(shù)據(jù)交換格式,占寬帶比較大。
JSON是輕量級交換格式,xml占寬帶小。
所有很多互聯(lián)網(wǎng)公司都會使用json作為數(shù)據(jù)交換格式
很多銀行項(xiàng)目,大多數(shù)還是在使用xml。
?
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的dom4kj解析xml的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springiocxml方式注入对象原理
- 下一篇: 手写springiocxml方式注入对象