8.xpath(dom4j支持的jar)


1.使用dom4j支持xpath的操作(xpath可以直接獲取到某個元素)

  (1)第一種形式

    /AAA/DDD/BBB:表示一層一層的,AAA下面DDD下面的BBB元素

  (2)第二種形式

    //BBB:表示獲取所有元素名字是BBB的元素

  (3)第三種形式

    /*:獲取所有的元素

  (4)第四種形式

    BBB[1]:表示獲取第一個BBB元素

    BBB[last()]:表示獲取最后一個BBB元素

  (5)第五種形式

    //BBB[@id]:表示獲取所有名字是BBB並且元素上面有id屬性的元素

  (6)第六種形式

    //BBB[@id='b1']:表示獲取所有名字是BBB且元素上面有id屬性的值b1

 

2.使用dom4j支持xpath具體操作

  (1)默認情況下,dom4j是不支持xpath的,如果我們要使用xpath,那么必須把包導入項目中

    包名:jaxen-1.1-beta-6.jar

    位置:我們解壓后dom4j_jar包\dom4j-1.6.1\lib中

  (2)在dom4j里里面提供了兩個方法,用來支持xpath

    selectNodes("xpath表達式") 獲取多個元素,返回一個list<Node>集合

    selectSingNode("xpath表達式") 並獲取單個元素,返回一個Element

 

3.下面是一個簡單的查詢實例

  需求:獲取修改第一個name元素中的文本為老王

  xml文件如下

 

<?xml version="1.0" encoding="UTF-8"?>
<persion>
    <one>
        <id>1001</id>
        <name>張三</name>
        <age>18</age>
    </one>
    <two>
        <id>1002</id>
        <name>李四</name>
        <age>20</age>
    </two>
</persion>

 

 

 

  實現代碼如下:

  

package Day4;

import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        //獲取解析器
        SAXReader saxReader = new SAXReader();
        //獲取整個文檔document
        Document document = saxReader.read("src/Day4/1.xml");
        //直接獲取第一個name元素
        Node node = document.selectSingleNode("//name[1]");
        //修改name元素的值為老王
        node.setText("老王");
        //更新xml文件
        FileOutputStream writer = new  FileOutputStream("src/Day4/1.xml");
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, format);
        xmlWriter.write(document);
        xmlWriter.close();
    }
}

 

 

 

 

4.簡化上面的代碼

  可以把獲取解析器和document封裝

  可以把更新代碼封裝

  可以xml的相對地址封裝(這樣便於修改xml地址文件,這樣寫我們就不需要在實現帶碼中直接修改)

代碼如下:

 

package Day4;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Demo2 {
    public static void main(String[] args) {
        //獲取document
        Document document = GetDoument.getDocument(GetDoument.path);
        //直接獲取第一個name元素
        Node node = document.selectSingleNode("//name[1]");
        //修改name元素的值為老王
        node.setText("老王");
        //更新xml文件
        NewXml.newXml(document, GetDoument.path);
    }
}


//可以把獲取解析器和document封裝
class GetDoument{
    //封裝xml文件的相對地址
    public static final String path = "src/Day4/1.xml";
    
    public static Document getDocument(String path) {
        try {
            //獲取解析器
            SAXReader saxReader = new SAXReader();
            //獲取整個文檔document
            Document document = saxReader.read(path);
            return document;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }
}

//可以把更新代碼封裝
class NewXml{
    
    public static void newXml(Document document,String path) {
        FileOutputStream writer;
        try {
            writer = new  FileOutputStream(path);
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter xmlWriter = new XMLWriter(writer, format);
            xmlWriter.write(document);
            xmlWriter.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM