欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java讀取解析xml文件實(shí)例

 更新時(shí)間:2015年03月28日 10:16:11   投稿:junjie  
這篇文章主要介紹了java讀取解析xml文件實(shí)例,本文創(chuàng)建了一個(gè)XML解析類同時(shí)講解了循環(huán)節(jié)點(diǎn)輸出方式,需要的朋友可以參考下

讀取本地的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)文章

  • struts2獲取服務(wù)器臨時(shí)目錄的方法

    struts2獲取服務(wù)器臨時(shí)目錄的方法

    這篇文章主要為大家詳細(xì)介紹了struts2獲取服務(wù)器臨時(shí)目錄的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 解決Mac?m1?電腦?idea?卡頓的問(wèn)題

    解決Mac?m1?電腦?idea?卡頓的問(wèn)題

    這篇文章主要介紹了Mac?m1?電腦?idea?卡頓的問(wèn)題解決,文中給大家補(bǔ)充介紹了IDEA卡頓問(wèn)題處理方法,需要的朋友可以參考下
    2023-03-03
  • 談?wù)凧ava中的守護(hù)線程與普通線程

    談?wù)凧ava中的守護(hù)線程與普通線程

    這篇文章主要介紹了Java中的守護(hù)線程與普通線程,幫助大家更好的理解和學(xué)習(xí)Java 多線程,感興趣的朋友可以了解下
    2020-09-09
  • 從零開始學(xué)JAVA之可變參數(shù)

    從零開始學(xué)JAVA之可變參數(shù)

    本文是從零開始學(xué)JAVA的第一篇,屬于Java基礎(chǔ)知識(shí)介紹的第一部分,主要介紹Java的可變參數(shù),非常使用,希望對(duì)大家有所幫助
    2014-10-10
  • SpringBoot中Bean注入沖突的四種解決方案

    SpringBoot中Bean注入沖突的四種解決方案

    在Spring?Boot應(yīng)用開發(fā)中,依賴注入是最常用的功能之一,它極大地簡(jiǎn)化了對(duì)象之間的依賴關(guān)系管理,然而,當(dāng)Spring容器中存在多個(gè)類型相同的Bean時(shí),就會(huì)產(chǎn)生注入沖突問(wèn)題,本文將介紹Spring?Boot中的四種Bean注入沖突解決方案,需要的朋友可以參考下
    2025-06-06
  • SpringMVC訪問(wèn)靜態(tài)資源的方法

    SpringMVC訪問(wèn)靜態(tài)資源的方法

    本篇文章主要介紹了SpringMVC訪問(wèn)靜態(tài)資源的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • 基于JDK8總結(jié)java中的interrupt

    基于JDK8總結(jié)java中的interrupt

    本文是基于JDK8總結(jié)java中的interrupt知識(shí),需要的朋友可以參考下
    2017-12-12
  • Spring注解配置IOC,DI的方法詳解

    Spring注解配置IOC,DI的方法詳解

    這篇文章主要為大家介紹了vue組件通信的幾種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • Spring MVC中日期格式轉(zhuǎn)換的兩種實(shí)用方法

    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-08
  • java對(duì)象池管理方式common-pool2使用

    java對(duì)象池管理方式common-pool2使用

    這篇文章主要為大家介紹了java對(duì)象池common-pool2使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05

最新評(píng)論