Java實(shí)現(xiàn)解析并生成xml原理實(shí)例詳解
XML解析:
* 解析xml可以做: * 如果xml作為配置文件:讀取 * 如果xml作為傳輸文件:寫,讀xml解析思想:
* DOM:將文檔加載進(jìn)內(nèi)存,形成一顆dom樹(document對(duì)象),將文檔的各個(gè)組成部分封裝為一些對(duì)象。 * 優(yōu)點(diǎn):因?yàn)?,在?nèi)存中會(huì)形成dom樹,可以對(duì)dom樹進(jìn)行增刪改查。 * 缺點(diǎn):dom樹非常占內(nèi)存,解析速度慢。DocumentElementTextAttributeComment
* SAX:逐行讀取,基于事件驅(qū)動(dòng)* 優(yōu)點(diǎn):不占內(nèi)存,速度快* 缺點(diǎn):只能讀取,不能回寫
xml常用的解析器:
* JAXP:sun公司提供的解析。支持dom和sax。(不常用) * JDOM: * DOM4J:dom for java民間方式,但是是事實(shí)方式。非常好。 支持dom1.導(dǎo)入jar包 dom4j.jar
2.創(chuàng)建解析器
SAXReader reader = new SAXReader();
3.解析xml 獲得document對(duì)象
Document document = reader.read(url);
* XPATH:專門用于查詢 * 定義了一種規(guī)則。 * 使用的方法: * selectSingleNode(): * selectNodes():使用步驟:
1、注意:要導(dǎo)包 jaxen...jar
2、創(chuàng)建解析器
SAXReader reader = new SAXReader();
3、解析xml 獲得document對(duì)象
Document document = reader.read(url);
* 解析XML:
// 1、得到某個(gè)具體的節(jié)點(diǎn)內(nèi)容:第2本書的書名--》葵花寶典
// 2、遍歷所有元素節(jié)點(diǎn)
XPath:
// nodename 選取此節(jié)點(diǎn)。 // / 從根節(jié)點(diǎn)選取。 // // 從匹配選擇的當(dāng)前節(jié)點(diǎn)選擇文檔中的節(jié)點(diǎn),而不考慮它們的位置。 // .. 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)。 // @ 選取屬性。 // [@屬性名] 屬性過濾 // [標(biāo)簽名] 子元素過濾例子:
/* demo.xml<?xml version='1.0' encoding='UTF-8'?><books> <book name='Harry Potter'> <author>J.K. Rowling</author> <price>29.90$</price> </book> <book name='冰與火之歌'> <author>馬丁</author> <price>28.90$</price> </book></books>*/@Testpublic void test1() throws DocumentException{ SAXReader reader = new SAXReader(); Document doc = reader.read('./src/demo.xml'); Element root = doc.getRootElement(); Element book1 = root.element('book'); System.out.println(book1.getPath()); System.out.println(book1.elementText('price')); List<?> list = root.elements('book'); Element book2 = (Element) list.get(1); System.out.println(book2.attributeValue('name')); for(Iterator<?> it = book1.elementIterator();it.hasNext() ; ){ Element ele = (Element) it.next(); System.out.println(ele.getName()+' : '+ele.getText()); }}@Test public void test2() throws Exception{//使用 XPath,只用于快速查詢,需要用jaxen...jar包 SAXReader reader = new SAXReader(); Document doc = reader.read(new File('src/demo.xml')); Node node = doc.selectSingleNode('/books/book[2]/author');//需要jaxen.jar包 String s = node.getText(); System.out.println(s); node = doc.selectSingleNode('/books/book[2]/@name'); s = node.getText(); System.out.println(s); List<Node> booknames = doc.selectNodes('/books//@name'); for (Node node1 : booknames) { System.out.println(node1.getText()); } List<?> nodes = doc.selectNodes('/books/*/*'); for (int i=0; i<nodes.size(); ++i){ Node node1 = (Node) nodes.get(i); System.out.println(node1.getName() + ': ' + node1.getText()); }}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼2. python 浮點(diǎn)數(shù)四舍五入需要注意的地方3. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法4. python開發(fā)一款翻譯工具5. 利用CSS制作3D動(dòng)畫6. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))7. Springboot 全局日期格式化處理的實(shí)現(xiàn)8. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題9. SpringBoot+TestNG單元測試的實(shí)現(xiàn)10. PHP實(shí)現(xiàn)簡單線性回歸之?dāng)?shù)學(xué)庫的重要性
