ITeye博客

怎样用Java读取XML文件--(Dom解析器)

rogerhunt · 2013-04-03 14:02 · 核心Java文档 · 编程语言 · Markdown

在这篇文章里,你将看到怎样用DOM XML解析器读取XML文件。DOM解析器解析整个XML文件并把它加载到内存中;然后模式化为一个树结构以便于便利或操作。 总之,就是把转换成了一个[url=http://www.w3schools.com/dom/default.asp#gsc.tab=0]DOM[/url]对象或者树结构,并且必须一个节点一个节点地遍历以取得你想要的节点。 [quote] 什么是节点(Node)? 对DOM来说,XML里的所有东西都可以看作一个节点,[url=http://www.w3schools.com/dom/dom_nodes.asp]请查看[/url]。 [/quote] [quote] 警告 当DOM加载一个含有大量数据的XML文档时,速度缓慢并且消费大量的内存。请考虑使用SAX解析器替代,SAX比DOM更快并且使用更少内存。 [/quote] [b]1.DOM XML解析器例子[/b] 通过这个例子你将知道如何通过"name"取得节点,并且显示值。 [code="xml"] Users/tongxiqing/staff.xml 政 赢 秦始皇 100000 邦 刘 汉高祖 200000 [/code] [code="java"] ReadXMLFile.java /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.mkyong.seo; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; /** * * @author Administrator */ public class ReadXMLFile { public static void main(String argv[]) { try { File fXmlFile = new File("src/Users/tongxiqing/staff.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); //optional, but recommended //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work doc.getDocumentElement().normalize(); System.out.println("根节点 :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("staff"); System.out.println("----------------------------"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\n当前节点 :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("职工号 : " + eElement.getAttribute("id")); System.out.println("名 : " + eElement.getElementsByTagName("firstname").item(0).getTextContent()); System.out.println("姓 : " + eElement.getElementsByTagName("lastname").item(0).getTextContent()); System.out.println("号 : " + eElement.getElementsByTagName("nickname").item(0).getTextContent()); System.out.println("俸禄 : " + eElement.getElementsByTagName("salary").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } } } [/code] 结果 [code="txt"] run: 根节点 :company ---------------------------- 当前节点 :staff 职工号 : 1001 名 : 政 姓 : 赢 号 : 秦始皇 俸禄 : 100000 当前节点 :staff 职工号 : 2001 名 : 邦 姓 : 刘 号 : 汉高祖 俸禄 : 200000 成功构建 (总时间: 0 秒) [/code] 2. 循环遍历节点 本例子读取“staff1.xml“文件,并且你将看到如何一个一个地遍历节点,同时输出名字和值,如果有属性的话还可以输出属性。 [code="xml"] Users/tongxiqing/staff1.xml <公司> <职员 编号="1001"> <姓>政 <名>赢 <号>秦始皇 <俸禄>100000 <职员 编号="2001"> <姓>邦 <名>刘 <号>汉高祖 <俸禄>200000 [/code] [code="java"] ReadXMLFile2.java /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.mkyong.seo; 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; /** * * @author Administrator */ public class ReadXMLFile2 { public static void main(String[] args) { try { File file = new File("src/Users/tongxiqing/staff1.xml"); DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = dBuilder.parse(file); System.out.println("根节点 :" + doc.getDocumentElement().getNodeName()); if (doc.hasChildNodes()) { printNote(doc.getChildNodes()); } } catch (Exception e) { System.out.println(e.getMessage()); } } private static void printNote(NodeList nodeList) { for (int count = 0; count < nodeList.getLength(); count++) { Node tempNode = nodeList.item(count); // make sure it's element node. if (tempNode.getNodeType() == Node.ELEMENT_NODE) { // get node name and value System.out.println("\n节点名字 =" + tempNode.getNodeName() + " [OPEN]"); System.out.println("节点值 =" + tempNode.getTextContent()); if (tempNode.hasAttributes()) { // get attributes names and values NamedNodeMap nodeMap = tempNode.getAttributes(); for (int i = 0; i < nodeMap.getLength(); i++) { Node node = nodeMap.item(i); System.out.println("属性名字 : " + node.getNodeName()); System.out.println("属性值 : " + node.getNodeValue()); } } if (tempNode.hasChildNodes()) { // loop again if has child nodes printNote(tempNode.getChildNodes()); } System.out.println("节点名字 =" + tempNode.getNodeName() + " [CLOSE]"); } } } } [/code] 结果 [code="txt"] run: 根节点 :公司 节点名字 =公司 [OPEN] 节点值 = 政 赢 秦始皇 100000 邦 刘 汉高祖 200000 节点名字 =职员 [OPEN] 节点值 = 政 赢 秦始皇 100000 属性名字 : 编号 属性值 : 1001 节点名字 =姓 [OPEN] 节点值 =政 节点名字 =姓 [CLOSE] 节点名字 =名 [OPEN] 节点值 =赢 节点名字 =名 [CLOSE] 节点名字 =号 [OPEN] 节点值 =秦始皇 节点名字 =号 [CLOSE] 节点名字 =俸禄 [OPEN] 节点值 =100000 节点名字 =俸禄 [CLOSE] 节点名字 =职员 [CLOSE] 节点名字 =职员 [OPEN] 节点值 = 邦 刘 汉高祖 200000 属性名字 : 编号 属性值 : 2001 节点名字 =姓 [OPEN] 节点值 =邦 节点名字 =姓 [CLOSE] 节点名字 =名 [OPEN] 节点值 =刘 节点名字 =名 [CLOSE] 节点名字 =号 [OPEN] 节点值 =汉高祖 节点名字 =号 [CLOSE] 节点名字 =俸禄 [OPEN] 节点值 =200000 节点名字 =俸禄 [CLOSE] 节点名字 =职员 [CLOSE] 节点名字 =公司 [CLOSE] 成功构建 (总时间: 0 秒) [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。