dom4j解析XML時使用XPath直接定位至標籤執行個體

來源:互聯網
上載者:User

IT程式員開發必備-各類資源下載清單,史上最全IT資源,個人收藏總結。 package  edu.dom4j.dom;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Element;

import org.dom4j.Node;

import org.dom4j.io.SAXReader;

import org.junit.Test;

public class XPathReadXml {

  private String xmlfile1 = "WebRoot/product2.xml";

  private String xmlfile2 = "WebRoot/users.xml";

public Document getDocument(String xmlfile) throws DocumentException{

  SAXReader reader = new SAXReader();

  return reader.read(xmlfile);

}

   //讀取<product>下<price>的Text值

   @Test

public void readText() throws DocumentException{

   Document document = getDocument(xmlfile1);

   //如果路徑以斜線 / 開始, 那麼該路徑就表示到一個元素的絕對路徑 

   String xmlPath1 = "/catalog/product/price";

   //如果路徑以雙斜線 // 開頭, 則表示選擇文檔中所有滿足雙斜線//之後規則的元素(無論層級關係) 

   

   String xmlPath2 = "//price";

   List<Element> priceList = document.selectNodes(xmlPath1);

   for (Element price : priceList) {

      System.out.println("<"+price.getName()+">"+price.getText()+"</"+price.getName()+">");

   }

   System.out.println("-------------------");

   Element product1 = document.getRootElement().element("product");

   //product1的效果和document查詢的效果是一樣的

   priceList = product1.selectNodes(xmlPath1);

   for (Element price : priceList) {

      System.out.println("<"+price.getName()+">"+price.getText()+"</"+price.getName()+">");

   }

   System.out.println("-------------------");

   Element price1 = (Element) document.selectSingleNode(xmlPath1);

   if(price1!=null)

      System.out.println("<"+price1.getName()+">"+price1.getText()+"</"+price1.getName()+">");

 

   System.out.println("-------------------");

   price1 = (Element) document.selectSingleNode(xmlPath2);

   System.out.println("<"+price1.getName()+">"+price1.getText()+"</"+price1.getName()+">");

   System.out.println("-------------------");

   priceList = document.selectNodes(xmlPath2);

   for (Element price : priceList) {

      System.out.println("<"+price.getName()+">"+price.getText()+"</"+price.getName()+">");

   }

   /**

    * 輸出結果: 

     <price>80.0</price>

     <price>100.0</price>

     -------------------

     <price>80.0</price>

     <price>100.0</price>

     -------------------

     <price>80.0</price>

     -------------------

     <price>80.0</price>

     -------------------

     <price>80.0</price>

     <price>100.0</price>

    **/

   }

   //驗證使用者登陸是否成功

   @Test

   public void valideLogin() throws DocumentException{

    Document document = getDocument(xmlfile2);

    String username = "aaa";

    String password = "aaapass";

    

    String xmlpath = "//user[@name='"+username+"' and @password='"+password+"']";

    Node node = document.selectSingleNode(xmlpath);

    if (node!=null) {

      System.out.println("登陸成功。");

    }else {

     System.out.println("使用者名稱和密碼不存在。");

    }

    

}

}

product2.xml檔案:

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

<catalog id="cata1"> 

  <product category="HandTool" inventory="InStock"> 

    <specifications weight="2.0kg">扳手</specifications>  

    <price street="香港街">80.0</price>  

    <notes>這是扳手</notes>  

  </product>  

  <product category="Table"> 

    <specifications>桌椅</specifications>  

    <price street="澳門街" wholesale="部分">100.0</price> 

  </product> 

</catalog>

users.xml檔案:

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

<users>

    <user name="aaa" password="aaapass"></user>

    <user name="bbb" password="bbbpass"></user>

    <user name="ccc" password="cccpass"></user>

    <user name="ddd" password="dddpass"></user>

</users>


關於XPath定位的用法情參見:XPathTutorial.chm(下載地址:http://download.csdn.net/detail/xh16319/4587406)



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.