How To Read XML File In Java – (DOM Parser)
发布时间:2020-12-16 05:19:59 所属栏目:百科 来源:网络整理
导读:本教程,将展现使用 DOM XML 解析技术,读取 XML 文件 DOM 解析器,将解析整个 XML 文档并且将把所有的 XML文档信息 加载到内存当中,然后再进行操作 简言之,此技术,将 XML 文件转换成 DOM 或者说是 树形结构,然后你就可以遍历结点了。 什么是节点? 在DO
本教程,将展现使用 DOM XML 解析技术,读取 XML 文件 DOM 解析器,将解析整个 XML 文档并且将把所有的 XML文档信息 加载到内存当中,然后再进行操作 简言之,此技术,将 XML 文件转换成 DOM 或者说是 树形结构,然后你就可以遍历结点了。 什么是节点? 在DOM中, XML文档 中的所有信息,都是节点。 例如:<salary>200000</salary> salary是节点,200000也是节点,200000是文本节点。200000不是salary的值。 看定义:http://www.w3schools.com/dom/dom_nodes.asp Text is Always Stored in Text Nodes 警告(提示): DOM解析技术,速度慢,消耗很多内存. 对于大文件,通常使用 SAX技术,SAX 快速,消耗内存少。 1. DOM XML Parser Example 本例子,通过“name”获得节点,以及对应的值 D:javaCodestaff.xml <?xml version="1.0" encoding="UTF-8"?> <company> <staff id="1001"> <firstname>xiaohan</firstname> <lastname>yi</lastname> <nickname>xiaokai</nickname> <salary>100000</salary> </staff> <staff id="2001"> <firstname>xiaowei</firstname> <lastname>lan</lastname> <nickname>xiaowei</nickname> <salary>200000</salary> </staff> </company> package com.domdemo; 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; public class ReadXMLFile { public static void main(String argv[]) { try { File fXmlFile = new File("D:/javaCode/staff.xml"); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = documentBuilderFactory.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("Root element :" + 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("nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("Staff id : "+ eElement.getAttribute("id")); System.out.println("First Name : "+ eElement.getElementsByTagName("firstname").item(0).getTextContent()); System.out.println("Last Name : "+ eElement.getElementsByTagName("lastname").item(0).getTextContent()); System.out.println("Nick Name : "+ eElement.getElementsByTagName("nickname").item(0).getTextContent()); System.out.println("Salary : "+ eElement.getElementsByTagName("salary").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } } } 运行结果:
Root element :company -------------我是分隔线------------------------------------------- Current Element :staff Staff id : 1001 First Name : xiaohan Last Name : yi Nick Name : xiaokai Salary : 100000 Current Element :staff Staff id : 2001 First Name : xiaowei Last Name : lan Nick Name : xiaowei Salary : 200000 2. Looping the Node 本例子读取同样的“staff.xml“,将展现如何一个接一个的循环节点,并且打印出节点名和值,如果有属性的话,也将属性打印出。 package com.domdemo; 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 ReadXMLFileLoopNode { public static void main(String[] args) { try { File file = new File("D:/javaCode/staff.xml"); DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = dBuilder.parse(file); System.out.println("Root element :" + 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) { // 获取节点名,和节点值 System.out.println("nNode Name =" + tempNode.getNodeName() + " [OPEN]"); System.out.println("Node Value =" + tempNode.getTextContent()); if (tempNode.hasAttributes()) { // 获取属性名,和属性值 NamedNodeMap nodeMap = tempNode.getAttributes(); for (int i = 0; i < nodeMap.getLength(); i++) { Node node = nodeMap.item(i); System.out.println("attr name : " + node.getNodeName()); System.out.println("attr value : " + node.getNodeValue()); } } if (tempNode.hasChildNodes()) { // loop again if has child nodes printNote(tempNode.getChildNodes()); } System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]"); } } } } Root element :company Node Name =company [OPEN] Node Value = xiaohan yi xiaokai 100000 xiaowei lan xiaowei 200000 Node Name =staff [OPEN] Node Value = xiaohan yi xiaokai 100000 attr name : id attr value : 1001 Node Name =firstname [OPEN] Node Value =xiaohan Node Name =firstname [CLOSE] Node Name =lastname [OPEN] Node Value =yi Node Name =lastname [CLOSE] Node Name =nickname [OPEN] Node Value =xiaokai Node Name =nickname [CLOSE] Node Name =salary [OPEN] Node Value =100000 Node Name =salary [CLOSE] Node Name =staff [CLOSE] Node Name =staff [OPEN] Node Value = xiaowei lan xiaowei 200000 attr name : id attr value : 2001 Node Name =firstname [OPEN] Node Value =xiaowei Node Name =firstname [CLOSE] Node Name =lastname [OPEN] Node Value =lan Node Name =lastname [CLOSE] Node Name =nickname [OPEN] Node Value =xiaowei Node Name =nickname [CLOSE] Node Name =salary [OPEN] Node Value =200000 Node Name =salary [CLOSE] Node Name =staff [CLOSE] Node Name =company [CLOSE]
参考文献: http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |