一 解析xml文件如下:
<?xml version="1.0" encoding="UTF-8"?><students> <student1 id="001"> <微信公眾號(hào)>@殘缺的孤獨(dú)</微信公眾號(hào)> <學(xué)號(hào)>20140101</學(xué)號(hào)> <地址>北京海淀區(qū)</地址> <座右銘>要么強(qiáng)大,要么聽(tīng)話</座右銘> <phone>137xxxxxxxx</phone> <phone>137xxxxxxxx</phone> <QQ>786363617@qq.com</qq> </student1> <student2 id="002"> <新浪微博>@殘缺的孤獨(dú)</新浪微博> <學(xué)號(hào)>20140102</學(xué)號(hào)> <地址>北京朝陽(yáng)區(qū)</地址> <座右銘>在哭泣中學(xué)會(huì)堅(jiān)強(qiáng)</座右銘> </student2> </students>
二 :解析xml步驟如下
/** * */package cn.com.yy.dom4j;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.util.Iterator;import java.util.List;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;import org.junit.Test;/** * @author niu * */public class Dom4JforXML {/*@Testpublic void test() throws DocumentException{//創(chuàng)建SAXReader對(duì)象 SAXReader reader = new SAXReader();//讀取文件 轉(zhuǎn)換成DocumentDocument document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));//獲取根節(jié)點(diǎn)元素對(duì)象Element root = document.getRootElement();//遍歷listNode(root);}*///遍歷當(dāng)前節(jié)點(diǎn)下所有的節(jié)點(diǎn)public void listNode(Element root) {// TODO Auto-generated method stubSystem.out.PRintln("當(dāng)前節(jié)點(diǎn)的名稱"+root.getName());//首先獲取當(dāng)前節(jié)點(diǎn)的所有屬性節(jié)點(diǎn)List<Attribute> list = root.attributes();//遍歷屬性節(jié)點(diǎn)for (Attribute attribute : list) {System.out.println("屬性:"+attribute.getName()+":"+attribute.getValue());}//如果當(dāng)前節(jié)點(diǎn)內(nèi)容不為空,則輸出if (!(root.getTextTrim().equals(""))) {System.out.println(root.getName()+":"+root.getText());}//同時(shí)迭代當(dāng)前節(jié)點(diǎn)下面的所有子節(jié)點(diǎn)Iterator<Element> iterator = root.elementIterator();while (iterator.hasNext()) {Element e = iterator.next();listNode(e);}}//添加節(jié)點(diǎn)屬性、刪除節(jié)點(diǎn)屬性、修改屬性值/* @Testpublic void test2() throws DocumentException{SAXReader reader = new SAXReader();Document document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));Element root = document.getRootElement();System.out.println("--------添加屬性前-------------------");Element student1Element = root.element("student1");listNode(student1Element);Attribute idAttribute = student1Element.attribute("id");student1Element.remove(idAttribute);student1Element.addAttribute("name", "這是student1節(jié)點(diǎn)的新屬性");System.out.println("-----------------添加新屬性后----------------");listNode(student1Element);}*///刪除指定節(jié)點(diǎn),新增節(jié)點(diǎn)操作@Testpublic void test3() throws DocumentException, IOException{//創(chuàng)建SAXReader對(duì)象 SAXReader reader = new SAXReader(); //讀取文件 轉(zhuǎn)換成Document Document document = reader.read(new File("src/cn/com/yy/dom4j/s.xml")); //獲取根節(jié)點(diǎn)元素對(duì)象 Element root = document.getRootElement(); System.out.println("-------添加節(jié)點(diǎn)前------"); //獲取節(jié)點(diǎn)student1 Element student1Element = root.element("student1"); //遍歷 listNode(student1Element); //添加phone節(jié)點(diǎn) Element phoneElement = student1Element.addElement("phone"); Element qqElement = student1Element.addElement("qq"); //為phone節(jié)點(diǎn)設(shè)置值 phoneElement.setText("137xxxxxxxx"); qqElement.setText("786363617@qq.com"); System.out.println("-------添加節(jié)點(diǎn)后------"); listNode(student1Element); //把student1Element寫入新文件 writerDocumentToNewFile(document); System.out.println("---寫入完畢----"); }public void writerDocumentToNewFile(Document document) throws IOException {// TODO Auto-generated method stub//輸出格式OutputFormat format= OutputFormat.createPrettyPrint();//設(shè)置編碼format.setEncoding("UTF-8");//XMLWriter 指定輸出文件以及格式 XMLWriter write = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("src/cn/com/yy/dom4j/s.xml")),"UTF-8"),format);//寫入新文件write.write(document);write.flush();write.close();}}
三 解析xml有五種方法并比較如下
xml解析五種方法為 DOM SAX JDOM DOM4j XPath
xml五種方法比較如下:1、
【DOM】DOM是基于樹(shù)的結(jié)構(gòu),通常需要加載整文檔和構(gòu)造DOM樹(shù),然后才能開(kāi)始工作。優(yōu)點(diǎn): a、由于整棵樹(shù)在內(nèi)存中,因此可以對(duì)xml文檔隨機(jī)訪問(wèn) b、可以對(duì)xml文檔進(jìn)行修改操作 c、較sax,dom使用也更簡(jiǎn)單。缺點(diǎn): a、整個(gè)文檔必須一次性解析完 a、由于整個(gè)文檔都需要載入內(nèi)存,對(duì)于大文檔成本高
2、【SAX】SAX類似流媒體,它基于事件驅(qū)動(dòng)的,因此無(wú)需將整個(gè)文檔載入內(nèi)存,使用者只需要監(jiān)聽(tīng)自己感興趣的事件即可。優(yōu)點(diǎn): a、無(wú)需將整個(gè)xml文檔載入內(nèi)存,因此消耗內(nèi)存少 b、可以注冊(cè)多個(gè)ContentHandler缺點(diǎn): a、不能隨機(jī)的訪問(wèn)xml中的節(jié)點(diǎn) b、不能修改文檔
3、【JDOM】JDOM是純Java的處理XML的API,其API中大量使用Collections類,優(yōu)點(diǎn): a、DOM方式的優(yōu)點(diǎn) b、具有SAX的Java規(guī)則缺點(diǎn) a、DOM方式的缺點(diǎn)
4、【DOM4J】這4中xml解析方式中,最優(yōu)秀的一個(gè),集易用和性能于一身。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注