Xpath 是一種比較好用的xml文檔解析工具。相比較於DOM和SAX,Xpath主要用於在解析以前已經知道要解析的的節點名稱和屬性(貌似其餘兩種也是呀)。java
1.節點類型node
XPath中有七種結點類型:元素、屬性、文本、命名空間、處理指令、註釋以及文檔節點(或稱爲根節點)。文檔中存在元素節點,屬性節點,根節點dom
2.經常使用路徑表達式工具
表達式 | 描述 |
節點名稱(nodename) | 選取此節點的全部子節點 |
/ | 從根節點選取 |
// | 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置 |
. | 選取當前節點 |
.. | 選取當前節點的父節點 |
@ | 選取屬性 |
示例以下:ui
//@lang | 選取全部名爲 lang 的屬性 |
3.限定語this
用來查找某個特定的節點或者包含某個指定的值的節點。以方括號括起lua
//book[price>35.00] | 選擇全部book 元素,且其中的 price 元素的值須大於 35.00 |
/bookstore/book[1] | 選取屬於 bookstore 子元素的第一個 book 元素。 |
/bookstore/book[last()] | 選取屬於 bookstore 子元素的最後一個 book 元素。 |
/bookstore/book[last()-1] | 選取屬於 bookstore 子元素的倒數第二個 book 元素。 |
/bookstore/book[position()<3] | 選取最前面的兩個屬於 bookstore 元素的子元素的 book 元素。 |
//title[@lang] | 選取全部擁有名爲 lang 的屬性的 title 元素。 |
//title[@lang='eng'] | 選取全部 title 元素,且這些元素擁有值爲 eng 的 lang 屬性。 |
/bookstore/book[price>35.00] | 選取全部 bookstore 元素的 book 元素,且其中的 price 元素的值須大於 35.00。 |
/bookstore/book[price>35.00]/title | 選取全部 bookstore 元素中的 book 元素的 title 元素,且其中的 price 元素的值須大於 35.00。 |
4 .通配符spa
通配符 | 描述 |
* | 匹配任何元素節點 |
@* | 匹配任何屬性節點 |
node() | 匹配任何類型的節點 |
| | 選取若干路徑 |
使用示例 | |
路徑表達式 | 結果 |
/bookstore/* | 選取 bookstore 元素的全部子節點 |
//* | 選取文檔中的全部元素 |
//title[@*] | 選取全部帶有屬性的 title 元素。 |
//book/title | //book/price | 選取全部 book 元素的 tilte 和 price 元素。 |
//title | //price | 選取全部文檔中的 title 和 price 元素。 |
/bookstore/book/title | //price | 選取全部屬於 bookstore 元素的 book 元素的 title 元素,以及文檔中全部的 price 元素 |
有個問題。.net
XPathConstants
這個類的具體做用是什麼?xml
無論了,等用到再說吧。
package per.XMLFileParse; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import java.io.File; /** * Created by li on 2016/11/14. * 解析xml文件的工具類。 */ public class XMLParser { private String path; /** * 構造器,要解析xml文檔,首先必須先指導xml文檔在哪吧。 */ public XMLParser(String path) { this.path = path; } /** * 傳入一個xml文件的路徑,獲取Document對象 * param path; xml 文件路徑。 * return document xml 的樹節點對象。 **/ static Document getDocument(String path) { Document document = null; try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(false); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.parse(new File(path)); } catch (Exception e) { System.out.println("獲取xml文檔節點失敗。"); } return document; } /** * 建立一個用於解析文檔的xpath對象的方法。 * param ;null * return XPath對象 */ static XPath getXPath() { XPathFactory xPathFactory = XPathFactory.newInstance(); return xPathFactory.newXPath(); } public static void main(String[] args) { Node node = getSpecifyNode("G:\\completeTheAim\\god\\MyProject\\src\\main\\java\\per\\myproject\\interaction\\set_hi_wtgl_fjxx.xml", "/service/inpoutData/vo"); System.out.println(node.hasChildNodes()); NamedNodeMap namedNodeMap = node.getAttributes(); System.out.println(namedNodeMap.item(1).getNodeValue()); System.out.println(namedNodeMap.getLength()); } /** * 工具方法,用於xml文檔的獲取根元素。 */ static Node getRootNode(String path) { Document document = getDocument(path); XPath xPath = getXPath(); Node node = null; try { node = (Node) xPath.evaluate("/*", document, XPathConstants.NODE); } catch (XPathExpressionException e) { System.out.println("獲取xml文檔根節點失敗。"); } return node; } /** * 輸入xml文件的path和文檔中指定路徑,得到那個節點。 * param:filePath xml文件路徑 * param:nodePath 節點路徑 * return:node 指定path的node */ static Node getSpecifyNode(String filePath, String nodePath) { Document document = getDocument(filePath); XPath xPath = getXPath(); Node node = null; try { node = (Node) xPath.evaluate(nodePath, document, XPathConstants.NODE); } catch (XPathExpressionException e) { System.out.println("獲取xml文檔根節點失敗。"); } return node; } }
<?xml version="1.0" encoding="UTF-8"?> <service class="com.hi.problem.service.PM_GET_XMXX"> <inpoutData> <vo type="HI_WTGL_XMXX" name="in" /> </inpoutData> <outputData> <vo type="HI_WTGL_XMXX" name="out" /> </outputData> </service>