1629. 按鍵持續(xù)時間最長的鍵
1125
2025-04-01
DOM4J
與利用DOM、SAX、JAXP機(jī)制來解析xml相比,DOM4J 表現(xiàn)更優(yōu)秀,具有性能優(yōu)異、功能強大和極端易用使用的特點,只要懂得DOM基本概念,就可以通過dom4j的api文檔來解析xml。dom4j是一套開源的api。實際項目中,往往選擇dom4j來作為解析xml的利器。
先來看看dom4j中對應(yīng)XML的DOM樹建立的繼承關(guān)系
針對于XML標(biāo)準(zhǔn)定義,對應(yīng)于圖2-1列出的內(nèi)容,dom4j提供了以下實現(xiàn):
同時,dom4j的NodeType枚舉實現(xiàn)了XML規(guī)范中定義的node類型。如此可以在遍歷xml文檔的時候通過常量來判斷節(jié)點類型了。
常用API
class org.dom4j.io.SAXReader
read? 提供多種讀取xml文件的方式,返回一個Domcument對象
interface org.dom4j.Document
iterator? 使用此法獲取node
getRootElement? 獲取根節(jié)點
interface org.dom4j.Node
getName? 獲取node名字,例如獲取根節(jié)點名稱為bookstore
getNodeType? 獲取node類型常量值,例如獲取到bookstore類型為1——Element
getNodeTypeName? 獲取node類型名稱,例如獲取到的bookstore類型名稱為Element
interface org.dom4j.Element
attributes? 返回該元素的屬性列表
attributeValue? 根據(jù)傳入的屬性名獲取屬性值
elementIterator? 返回包含子元素的迭代器
elements? 返回包含子元素的列表
interface org.dom4j.Attribute
getName? 獲取屬性名
getValue? 獲取屬性值
interface org.dom4j.Text
getText? 獲取Text節(jié)點值
interface org.dom4j.CDATA
getText? 獲取CDATA Section值
interface org.dom4j.Comment
getText? 獲取注釋
//先加入dom4j.jar包
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* @Title: TestDom4j.java
* @Package
* @Description: 解析xml字符串
* @author 無處不在
* @date 2012-11-20 下午05:14:05
* @version V1.0
*/
public class TestDom4j {
public void readStringXml(String xml) {
Document doc = null;
try {
// 讀取并解析XML文檔
// SAXReader就是一個管道,用一個流的方式,把xml文件讀出來
//
// SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文檔
// Document document = reader.read(new File("User.hbm.xml"));
// 下面的是通過解析xml字符串的
doc = DocumentHelper.parseText(xml); // 將字符串轉(zhuǎn)為XML
Element rootElt = doc.getRootElement(); // 獲取根節(jié)點
System.out.println("根節(jié)點:" + rootElt.getName()); // 拿到根節(jié)點的名稱
Iterator iter = rootElt.elementIterator("head"); // 獲取根節(jié)點下的子節(jié)點head
// 遍歷head節(jié)點
while (iter.hasNext()) {
Element recordEle = (Element) iter.next();
String title = recordEle.elementTextTrim("title"); // 拿到head節(jié)點下的子節(jié)點title值
System.out.println("title:" + title);
Iterator iters = recordEle.elementIterator("script"); // 獲取子節(jié)點head下的子節(jié)點script
// 遍歷Header節(jié)點下的Response節(jié)點
while (iters.hasNext()) {
Element itemEle = (Element) iters.next();
String username = itemEle.elementTextTrim("username"); // 拿到head下的子節(jié)點script下的字節(jié)點username的值
String password = itemEle.elementTextTrim("password");
System.out.println("username:" + username);
System.out.println("password:" + password);
}
}
Iterator iterss = rootElt.elementIterator("body"); ///獲取根節(jié)點下的子節(jié)點body
// 遍歷body節(jié)點
while (iterss.hasNext()) {
Element recordEless = (Element) iterss.next();
String result = recordEless.elementTextTrim("result"); // 拿到body節(jié)點下的子節(jié)點result值
System.out.println("result:" + result);
Iterator itersElIterator = recordEless.elementIterator("form"); // 獲取子節(jié)點body下的子節(jié)點form
// 遍歷Header節(jié)點下的Response節(jié)點
while (itersElIterator.hasNext()) {
Element itemEle = (Element) itersElIterator.next();
String banlce = itemEle.elementTextTrim("banlce"); // 拿到body下的子節(jié)點form下的字節(jié)點banlce的值
String subID = itemEle.elementTextTrim("subID");
System.out.println("banlce:" + banlce);
System.out.println("subID:" + subID);
}
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @description 將xml字符串轉(zhuǎn)換成map
* @param xml
* @return Map
*/
public static Map readStringXmlOut(String xml) {
Map map = new HashMap();
Document doc = null;
try {
// 將字符串轉(zhuǎn)為XML
doc = DocumentHelper.parseText(xml);
// 獲取根節(jié)點
Element rootElt = doc.getRootElement();
// 拿到根節(jié)點的名稱
System.out.println("根節(jié)點:" + rootElt.getName());
// 獲取根節(jié)點下的子節(jié)點head
Iterator iter = rootElt.elementIterator("head");
// 遍歷head節(jié)點
while (iter.hasNext()) {
Element recordEle = (Element) iter.next();
// 拿到head節(jié)點下的子節(jié)點title值
String title = recordEle.elementTextTrim("title");
System.out.println("title:" + title);
map.put("title", title);
// 獲取子節(jié)點head下的子節(jié)點script
Iterator iters = recordEle.elementIterator("script");
// 遍歷Header節(jié)點下的Response節(jié)點
while (iters.hasNext()) {
Element itemEle = (Element) iters.next();
// 拿到head下的子節(jié)點script下的字節(jié)點username的值
String username = itemEle.elementTextTrim("username");
String password = itemEle.elementTextTrim("password");
System.out.println("username:" + username);
System.out.println("password:" + password);
map.put("username", username);
map.put("password", password);
}
}
//獲取根節(jié)點下的子節(jié)點body
Iterator iterss = rootElt.elementIterator("body");
// 遍歷body節(jié)點
while (iterss.hasNext()) {
Element recordEless = (Element) iterss.next();
// 拿到body節(jié)點下的子節(jié)點result值
String result = recordEless.elementTextTrim("result");
System.out.println("result:" + result);
// 獲取子節(jié)點body下的子節(jié)點form
Iterator itersElIterator = recordEless.elementIterator("form");
// 遍歷Header節(jié)點下的Response節(jié)點
while (itersElIterator.hasNext()) {
Element itemEle = (Element) itersElIterator.next();
// 拿到body下的子節(jié)點form下的字節(jié)點banlce的值
String banlce = itemEle.elementTextTrim("banlce");
String subID = itemEle.elementTextTrim("subID");
System.out.println("banlce:" + banlce);
System.out.println("subID:" + subID);
map.put("result", result);
map.put("banlce", banlce);
map.put("subID", subID);
}
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
public static void main(String[] args) {
// 下面是需要解析的xml字符串例子
String xmlString = "" + "
" + "+ "" + ""
+ "
" + "/*
* Test2 test = new Test2(); test.readStringXml(xmlString);
*/
Map map = readStringXmlOut(xmlString);
Iterator iters = map.keySet().iterator();
while (iters.hasNext()) {
String key = iters.next().toString(); // 拿到鍵
String val = map.get(key).toString(); // 拿到值
System.out.println(key + "=" + val);
}
}
}
實例2:
/**
* 解析包含有DB連接信息的XML文件
* 格式必須符合如下規(guī)范:
* 1. 最多三級,每級的node名稱自定義;
* 2. 二級節(jié)點支持節(jié)點屬性,屬性將被視作子節(jié)點;
* 3. CDATA必須包含在節(jié)點中,不能單獨出現(xiàn)。
*
* 示例1——三級顯示:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* 示例2——節(jié)點屬性:
*
*
*
*
*
*
*
*
*
*
XML
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。