java讀取解析xml文件實(shí)例
讀取本地的xml文件,通過(guò)DOM進(jìn)行解析,DOM解析的特點(diǎn)就是把整個(gè)xml文件裝載入內(nèi)存中,形成一顆DOM樹形結(jié)構(gòu),樹結(jié)構(gòu)是方便遍歷和和操縱。
DOM解析的特性就是讀取xml文件轉(zhuǎn)換為 dom樹形結(jié)構(gòu),通過(guò)節(jié)點(diǎn)進(jìn)行遍歷。
這是W3c關(guān)于節(jié)點(diǎn)的概念
如果xml中包含有大量的數(shù)據(jù),由于dom一次性把xml裝入內(nèi)存中的特性,所以dom不適合于包含大量數(shù)據(jù)的xml解析。當(dāng)包含有大量xml的時(shí)候,用SAX進(jìn)行解析比較節(jié)省內(nèi)存。
下面是一個(gè)運(yùn)用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 -------------------------
以上是通過(guò)name獲得對(duì)應(yīng)的值,
下面利用循環(huán)節(jié)點(diǎn)的方式輸出:
循環(huán)節(jié)點(diǎn)輸出方式的代碼如下:
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é)點(diǎn)的問(wèn)題:
<book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book>
對(duì)于 book應(yīng)用:doc.getChildNodes() 得到一個(gè)NodeList其中NodeList的長(zhǎng)度為9
9個(gè)節(jié)點(diǎn)分別如下:
title節(jié)點(diǎn)
lang節(jié)點(diǎn)
Everyday節(jié)點(diǎn)
author節(jié)點(diǎn)
Giada De Laurentiis節(jié)點(diǎn)
year節(jié)點(diǎn)
2005節(jié)點(diǎn)
price節(jié)點(diǎn)
30.00節(jié)點(diǎn)
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
相關(guān)文章
Spring MVC中日期格式轉(zhuǎn)換的兩種實(shí)用方法
在開發(fā)基于 Spring MVC 的 Web 應(yīng)用時(shí),日期格式的轉(zhuǎn)換是一個(gè)常見(jiàn)的需求,本文將詳細(xì)介紹 Spring MVC 中兩種日期格式轉(zhuǎn)換的方法,包括創(chuàng)建過(guò)程和最終的運(yùn)行結(jié)果,需要的朋友可以參考下2025-08-08java對(duì)象池管理方式common-pool2使用
這篇文章主要為大家介紹了java對(duì)象池common-pool2使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05