使用Xpath读取xml文件

来源:互联网 发布:数控车床螺纹编程实例 编辑:程序博客网 时间:2024/06/03 17:30

使用XPath的步骤

以下是使用XPath解析器在解析文档时使用的步骤。

  • 导入XML相关的软件包。

  • 创建DocumentBuilder

  • 从文件或数据流创建一个文档

  • 创建XPath对象和XPath的路径表达式

  • 编译XPath表达式使用XPath.compile() ,并由XPath.evaluate()评估计算获得一个节点列表

  • 遍历节点列表。

  • 检查属性

  • 检查子元素

导入XML相关的软件包

1
2
3
4
5
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import java.io.*;

创建DocumentBuilder

1
2
3
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

从文件或数据流创建一个文档

1
2
3
4
5
StringBuilder xmlStringBuilder = new StringBuilder();
xmlStringBuilder.append("<?xml version="1.0"?> <class> </class>");
ByteArrayInputStream input =  new ByteArrayInputStream(
   xmlStringBuilder.toString().getBytes("UTF-8"));
Document doc = builder.parse(input);

构建XPath

1
XPath xPath =  XPathFactory.newInstance().newXPath();

准备路径表达式,并计算它


1
2
String expression = "/class/student";            
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);

遍历节点列表

1
2
3
for (int i = 0; i < nodeList.getLength(); i++) {
   Node nNode = nodeList.item(i);
   ...}

检查属性

1
2
3
4
//returns specific attribute
getAttribute("attributeName"); 
//returns a Map (table) of names/values
getAttributes();

检查子元素

1
2
3
4
//returns a list of subelements of specified name
getElementsByTagName("subelementName"); 
//returns a list of all child nodes
getChildNodes();

演示示例:

这里是我们需要分析输入文本文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0"?>
<class>
   <student rollno="393">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks>85</marks>
   </student>
   <student rollno="493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>vinni</nickname>
      <marks>95</marks>
   </student>
   <student rollno="593">
      <firstname>jasvir</firstname>
      <lastname>singh</lastname>
      <nickname>jazz</nickname>
      <marks>90</marks>
   </student>
</class>

 

演示示例:

XPathParserDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cn.sxt.xml;
 
import java.io.File;
import java.io.IOException;
 
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
 
public class XPathParserDemo {
   public static void main(String[] args) {
      try {
         File inputFile = new File("input.txt");
         DocumentBuilderFactory dbFactory 
            = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder;
 
         dBuilder = dbFactory.newDocumentBuilder();
 
         Document doc = dBuilder.parse(inputFile);
         doc.getDocumentElement().normalize();
 
         XPath xPath =  XPathFactory.newInstance().newXPath();
 
         String expression = "/class/student";          
         NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
         for (int i = 0; i < nodeList.getLength(); i++) {
            Node nNode = nodeList.item(i);
            System.out.println("\nCurrent Element :" 
               + nNode.getNodeName());
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
               Element eElement = (Element) nNode;
               System.out.println("Student roll no : " 
                  + eElement.getAttribute("rollno"));
               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("Marks : " 
                  + eElement
                     .getElementsByTagName("marks")
                     .item(0)
                     .getTextContent());
            }
         }
      catch (ParserConfigurationException e) {
         e.printStackTrace();
      catch (SAXException e) {
         e.printStackTrace();
      catch (IOException e) {
         e.printStackTrace();
      catch (XPathExpressionException e) {
         e.printStackTrace();
      }
   }
}

这将产生以下结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Current Element :student
Student roll no : 393
First Name : dinkar
Last Name : kad
Nick Name : dinkar
Marks : 85
 
Current Element :student
Student roll no : 493
First Name : Vaneet
Last Name : Gupta
Nick Name : vinni
Marks : 95
 
Current Element :student
Student roll no : 593
First Name : jasvir
Last Name : singh
Nick Name : jazz
Marks : 90
原创粉丝点击