Java 操纵XML之读取XML文件
Java 操纵XML之读取XML文件
一、JAVA DOM PARSER
DOM interfaces
The DOM defines several Java interfaces. Here are the most common interfaces:
Node - The base datatype of the DOM.
Element - The vast majority of the objects you'll deal with are Elements.
Attr Represents an attribute of an element.
Text The actual content of an Element or Attr.
Document Represents the entire XML document. A Document object is often referred to as a DOM tree.
Common DOM methods
When you are working with the DOM, there are several methods you'll use often:
Document.getDocumentElement() - Returns the root element of the document.
Node.getFirstChild() - Returns the first child of a given Node.
Node.getLastChild() - Returns the last child of a given Node.
Node.getNextSibling() - These methods return the next sibling of a given Node.
Node.getPreviousSibling() - These methods return the previous sibling of a given Node.
Node.getAttribute(attrName) - For a given Node, returns the attribute with the requested name.
二、源代码:ReadXmlFile.java
1 package cn.com.zfc.lesson26.xml; 2 3 import java.io.File; 4 5 import javax.xml.parsers.DocumentBuilder; 6 import javax.xml.parsers.DocumentBuilderFactory; 7 8 import org.w3c.dom.Document; 9 import org.w3c.dom.Element; 10 import org.w3c.dom.Node; 11 import org.w3c.dom.NodeList; 12 13 /** 14 * 使用JAVA DOM PARSER:读取 XML 文件 15 * 16 * @author zfc 17 * @date 2017年12月7日 下午7:17:24 18 */ 19 public class ReadXmlFile { 20 public static void main(String[] args) { 21 22 try { 23 String filePath = "I:\\code_workspace\\JavaSE_workspace\\JavaSE\\src\\cn\\com\\zfc\\lesson26\\xml\\ReadXmlFile.xml"; 24 // 1、创建 File 对象,映射 XML 文件 25 File file = new File(filePath); 26 // 2、创建 DocumentBuilderFactory 对象,用来创建 DocumentBuilder 对象 27 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 28 // 3、创建 DocumentBuilder 对象,用来将 XML 文件 转化为 Document 对象 29 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 30 // 4、创建 Document 对象,解析 XML 文件 31 Document document = documentBuilder.parse(file); 32 // 5、调用方法 33 document.getDocumentElement().normalize(); 34 // 6、获取根元素 35 String studentsName = document.getDocumentElement().getNodeName(); 36 System.out.println("根元素是:" + studentsName); 37 // 7、获取所有的 student 结点 38 NodeList students = document.getElementsByTagName("student"); 39 // 8、遍历所有的 student 结点 40 for (int i = 0; i < students.getLength(); i++) { 41 // 获取当前的结点 student 42 Node node = students.item(i); 43 System.out.println("当前元素是第" + (i + 1) + "个元素:" + node.getNodeName()); 44 // 判断当前的结点类型 45 if (node.getNodeType() == Node.ELEMENT_NODE) { 46 Element element = (Element) node; 47 // 获取当前结点的属性 id 48 System.out.println("id:" + element.getAttribute("id")); 49 // 获取当前结点 student 的子结点 name 结点 50 Element name = (Element) element.getElementsByTagName("name").item(0); 51 // 输出 name 结点的文本值 52 System.out.println("name:" + name.getTextContent()); 53 // 获取当前结点 student 的子结点 sex 结点 54 Element sex = (Element) element.getElementsByTagName("sex").item(0); 55 // 输出 sex 结点的文本值 56 System.out.println("sex:" + sex.getTextContent()); 57 } 58 } 59 } catch (Exception e) { 60 e.printStackTrace(); 61 } 62 } 63 }
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 2 <students> 3 <student id="student1"> 4 <name>Tom</name> 5 <sex>Female</sex> 6 </student> 7 <student id="student2"> 8 <name>Lucy</name> 9 <sex>Male</sex> 10 </student> 11 </students>
三、运行效果: