Java XPath解析器 - 解析XML文檔

使用XPath的步驟

以下是使用XPath解析器在解析文檔時使用的步驟。

  • 導入XML相關的軟件包。

  • 創建DocumentBuilder

  • 從文件或數據流創建一個文檔

  • 創建XPath對象和XPath的路徑表達式

  • 編譯XPath表達式使用XPath.compile() ,並由XPath.evaluate()評估計算獲得一個節點列表

  • 遍歷節點列表。

  • 檢查屬性

  • 檢查子元素

導入XML相關的軟件包

import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import javax.xml.xpath.*; import java.io.*;

創建DocumentBuilder

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder();

從文件或數據流創建一個文檔

StringBuilder xmlStringBuilder = new StringBuilder(); xmlStringBuilder.append(" "); ByteArrayInputStream input = new ByteArrayInputStream( xmlStringBuilder.toString().getBytes("UTF-8")); Document doc = builder.parse(input);

構建XPath

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); ... }

檢查屬性

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

檢查子元素

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

演示示例:

這裏是我們需要分析輸入文本文件:

dinkar kad dinkar 85 Vaneet Gupta vinni 95 jasvir singh jazz 90

演示示例:

XPathParserDemo.java

package com.yiibai.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(); } } }

這將產生以下結果:

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