1. 程式人生 > >(三)XML基礎(3)

(三)XML基礎(3)

ack cdata mage void 元素節點 images 包含 exce -1

五、XPath:快速定位到節點

  5.1  簡介

技術分享

技術分享

  5.2  語法

技術分享

  5.3  案例

  • XPath對有命名空間的xml文件和沒有命名空間的xml定位節點的方法是不一樣的,所以再對不同的xml需要進行不同的處理。
  • 使用前提: 導入技術分享

  • Test_Xpath.java
package dom;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Test_Xpath { public static void main(String[] args) { // Test_Xpath.testNo_Namespace(); Test_Xpath.test_Namespace(); } /** * 搜索有命名空間的xml文件 */ private static void test_Namespace() { File xmlFile
=new File("./src/student.xml"); SAXReader saxReader=new SAXReader(); Map<String,String> namespaceURIs=new HashMap<String, String>(); /** * 設置命名空間,註意在xml文件中命名空間的前綴可以為空(xmlns="http://www.example.org/student") * 但是在這裏不能為空,必須有前綴 */ namespaceURIs.put(
"stu", "http://www.example.org/student"); namespaceURIs.put("jdbc","http://www.example.org/jdbc"); saxReader.getDocumentFactory().setXPathNamespaceURIs(namespaceURIs); try { Document document=saxReader.read(xmlFile); Element rootElment=document.getRootElement(); Element parentElement=rootElment.element("root"); /** * 從document節點開始搜索——絕對路徑 "/other/root/student" * 中以/打頭表示以絕對路徑方法,而這個xml文件裏的根節點為other, * 這裏的other、root、student節點都是stu命名空間的xml文件裏的,所以要加stu前綴 */ List<Element> list = document.selectNodes("/stu:other/stu:root/stu:student"); // System.out.println(list.size()); /** * 從other路徑搜索——相對路徑 */ list = rootElment.selectNodes("stu:root/stu:student"); // System.out.println(list.size()); /** * 從root目錄(parentElement節點)搜索起_全文路徑, * 這裏的parentElemen換成其他節點比如document或者rootElement也是可以的, * 只要這個節點是被搜索節點的父節點即可 */ list = parentElement.selectNodes("//stu:student"); // System.out.println(list.size()); /** * 找到屬性id=03的節點 */ list = parentElement.selectNodes("/stu:other/stu:root/stu:student[@id=03]"); Element element_id03 = list.get(0); // System.out.println(element_id03.element("name").getText()); /** * 查找性別為男的節點,[email protected],[email protected] * 屬性不用加前綴外,元素節點必須加前綴 */ list = parentElement.selectNodes("stu:student[stu:sex=‘男‘]"); // System.out.println(list.size()); /** * 查找年齡在某一範圍的節點 */ list = parentElement.selectNodes("stu:student[stu:age>=15 and stu:age<50]"); // System.out.println(list.size()); /** * 查找名稱中有a字符的節點,相當於sql中的模糊查詢(like), * xpath中也有函數,這裏的contains(name,‘張三‘) 就是一個函數, * student[contains(name,‘張三‘)] : * 表示查找子節點為name且name的值含有‘張三’的student節點 */ list = parentElement.selectNodes("stu:student[contains(stu:name,‘張三‘)]"); // System.out.println(list.size()); /** * 查找名稱為包含a的字符,並且性別為男的,並且年齡在一個範圍內 */ list = parentElement.selectNodes("stu:student[contains(stu:name,‘張三‘) and stu:sex=‘男‘ and stu:age>=10 ]"); // System.out.println(list.size()); Element jdbcElement=(Element)document.selectSingleNode("/stu:other/jdbc:jdbcInfo"); System.out.println(jdbcElement.elementText("jdbcDriver")); System.out.println(jdbcElement.elementText("url")); System.out.println(jdbcElement.elementText("password")); System.out.println(jdbcElement.elementText("user")); } catch (DocumentException e) { e.printStackTrace(); } } /** * 搜索沒有命名空間的xml文件 */ private static void testNo_Namespace() { File xmlFile = new File("./src/no_namespace.xml"); SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(xmlFile); Element rootElment = document.getRootElement(); Element parentElement = (Element) rootElment.elements("root").get(0); /** * 從document節點開始搜索——絕對路徑 "/other/root/student" * 中以/打頭表示以絕對路徑方法,而這個xml文件裏的根節點為other */ List<Element> list = document.selectNodes("/other/root/student"); // System.out.println(list.size()); /** * 從other路徑搜索——相對路徑 */ list = rootElment.selectNodes("root/student"); // System.out.println(list.size()); /** * 從root目錄(parentElement節點)搜索起_全文路徑, * 這裏的parentElemen換成其他節點比如document或者rootElement也是可以的, * 只要這個節點是被搜索節點的父節點即可 */ list = parentElement.selectNodes("//student"); // System.out.println(list.size()); /** * 找到屬性id=03的節點 */ list = parentElement.selectNodes("/other/root/student[@id=03]"); Element element_id03 = list.get(0); // System.out.println(element_id03.element("name").getText()); /** * 查找性別為男的節點,[email protected],[email protected] */ list = parentElement.selectNodes("student[sex=‘男‘]"); // System.out.println(list.size()); /** * 查找年齡在某一範圍的節點 */ list = parentElement.selectNodes("student[age>=15 and age<50]"); // System.out.println(list.size()); /** * 查找名稱中有a字符的節點,相當於sql中的模糊查詢(like), * xpath中也有函數,這裏的contains(name,‘張三‘) 就是一個函數, * student[contains(name,‘張三‘)] : * 表示查找子節點為name且name的值含有‘張三’的student節點 */ list = parentElement.selectNodes("student[contains(name,‘張三‘)]"); // System.out.println(list.size()); /** * 查找名稱為包含a的字符,並且性別為男的,並且年齡在一個範圍內 */ list = parentElement.selectNodes("student[contains(name,‘張三‘) and sex=‘男‘ and age>=10 ]"); System.out.println(list.size()); } catch (DocumentException e) { e.printStackTrace(); } } }
  • student.xml

<?xml version="1.0" encoding="UTF-8"?>

<other xmlns="http://www.example.org/student" xmlns:jdbc="http://www.example.org/jdbc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/student student.xsd        http://www.example.org/jdbc jdbc.xsd ">
    <root>
        <student id="01">
            <name>張三</name>
            <age>15</age>
            <sex></sex>
            <content>張三備註</content>
        </student>
        <student id="02">
            <name>被修改後的張三</name>
            <age>25</age>
            <sex></sex>
            <content>李四備註</content>
        </student>
        <student id="03">
            <name>新增</name>
            <age>12</age>
            <sex></sex>
            <content><![CDATA[#$%^#$%#$^]]></content>
        </student>
        </root>
        <jdbc:jdbcInfo>
            <jdbc:jdbcDriver>com.mysql.jdbc.Driver</jdbc:jdbcDriver>
            <jdbc:url>jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&amp;characterEncoding=UTF-8</jdbc:url>
            <jdbc:user>root</jdbc:user>
            <jdbc:password></jdbc:password>
        </jdbc:jdbcInfo>
</other>
  • no_namespace.xml
<?xml version="1.0" encoding="UTF-8"?>

<other >  
    <root> 
        <student id="01"> 
            <name>張三</name>  
            <age>15</age>  
            <sex></sex>  
            <content>張三備註</content> 
        </student>  
        <student id="02"> 
            <name>被修改後的張三</name>  
            <age>25</age>  
            <sex></sex>  
            <content>李四備註</content> 
        </student>  
        <student id="03">
            <name>新增</name>
            <age>12</age>
            <sex></sex>
            <content><![CDATA[#$%^#$%#$^]]></content>
        </student>
    
    </root> 
</other>


(三)XML基礎(3)