首页 > 代码库 > apache commons JXPath 说明,使用JXPath访问java对象、集合和XML文件

apache commons JXPath 说明,使用JXPath访问java对象、集合和XML文件

Commons-JXPath 提供了使用Xpath语法操纵符合Java类命名规范的 JavaBeans的工具。也支持 maps, DOM 和其他对象模型。JXPath的主要功能在于一组java类库来使用XPath的方式访问符合JavaBeans规范的java类、java集合(Collections)、其他具有动态属性的对象(如Map、ServletContext等),同时提供了一套扩展机制使我们可以增加对这些对象之外的其他对象模型的支持。

如果你要访问的属性不是这个Java类的属性,那么执行过程中系统会报出一个违例--org.apache.commons.jxpath.JXPathException: No value for xpath: xxx(xxx是你要访问的属性的名称)。

 

这种情况对于程序的稳定性、健壮性是有害的,这时候我们应该使用JXPath提供的Lenient 访问模式来避免出现这样的情况,在Lenient 访问模式下,如果你访问了不存在的属性,系统会返回一个null,而不是抛出一个违例。要使用Lenient 访问模式非常简单,只需要在代码中增加context.setLenient(true)调用就可以了。

我们先看下代码:

 

对应的xml文件如下:

<?xmlversion="1.0" encoding="utf-8"?>

<project>  

   <compname>中国电信</compname>

  <应用程序配置参数>

          <人员名称>范芳铭</人员名称>

          <workNo>31000696</workNo>

    <考勤时间>

      <上班时间>08:30:00</上班时间>

      <下班时间>17:00:00</下班时间>

      <迟到矿工时间分界点>08:45:00</迟到矿工时间分界点>

    </考勤时间>

  </应用程序配置参数>

         <company code="101">telecom</company>

                   <name>中国电信</name>

         <company code="102">mobile</company>

                   <name>中国移动</name> 

         <company code="103">unit </company>

                   <name>中国联通</name>

</project>

对应的位置和代码在一起。

 

packagetest.ffm83.commons.jxpath;

 

importjava.net.URL;

importjava.util.HashMap;

importjava.util.Map;

 

importorg.apache.commons.jxpath.Container;

importorg.apache.commons.jxpath.JXPathContext;

importorg.apache.commons.jxpath.xml.DocumentContainer;

/**

 * commons jxpath 简单访问对象,MAP,XML文件等

 * @author 范芳铭

 */

public classXpathUsage {

         public static void main(String[] args){

                   XpathUsage usage = newXpathUsage();

                   usage.getJavaBean();

                   usage.getMapJavaBean();

                   usage.getXMLValue();

                  

         }

        

         public void getJavaBean(){

                   Tb_user user = newTb_user("范芳铭","ffm");

                   JXPathContext context =JXPathContext.newContext(user);

                   String username =(String)context.getValue("username");

                   System.out.println("利用JXPathContext获取属性 username:" + username);

                  

                   context.setLenient(true);//设置安全模式

                   System.out.println("读取不存在的属性:" + (String)context.getValue("不存在的属性"));

         }

        

         public void getMapJavaBean(){

                   Tb_user user = new Tb_user();

                   JXPathContext context =JXPathContext.newContext(user);

                   context.setLenient(true);

                   System.out.println("Map属性id:" +(Integer)context.getValue("myMap/id"));

                   System.out.println("Map属性key:" +(String)context.getValue("myMap/key"));      

         }

        

         //本方法的和configuration访问XML的方法非常类似

         public void getXMLValue(){

                   Tb_user user = new Tb_user();

                   JXPathContext context =JXPathContext.newContext(user);

                   context.setLenient(true);

                  

                   System.out.println("公司名称:" + (String)context.getValue(

                                       "xmls/project/compname"));

                   System.out.println("员工工号:" + (String)context.getValue(

                                       "xmls/project/应用程序配置参数/workNo"));

                   //通过JXPath访问xml内容时,如果访问属性,必须增加一个@符号,以示区别

                   System.out.println("公司名称:" + (String)context.getValue(

                                       "xmls/project/company[@code =‘102‘]/@code"));

                  

         }

        

         //内部嵌套类

         public class Tb_user {

                   private String username;

                   private String password;

                   private int age;

                   private Map myMap;

                   private Container xmls =null;

                  

                   public Tb_user(){

                            myMap = newHashMap();

                            myMap.put("id",newInteger(5));

                            myMap.put("key","ffm");

                           

                            getXmls();//初始化XML对象

                   }

 

                   public Tb_user(Stringusername, String password) {

                            this.username =username;

                            this.password =password;

                   }

                   public Tb_user(Stringusername, String password,int age) {

                            this.username =username;

                            this.password =password;

                            this.age = age;

                   }

 

                   public String getUsername() {

                            return username;

                   }

 

                   public voidsetUsername(String username) {

                            this.username =username;

                   }

 

                   public String getPassword() {

                            return password;

                   }

 

                   public voidsetPassword(String password) {

                            this.password =password;

                   }

                  

                  

                   public int getAge() {

                            return age;

                   }

 

                   public void setAge(int age) {

                            this.age = age;

                   }

                  

                  

 

                   public Map getMyMap() {

                            return myMap;

                   }

 

                   public void setMyMap(MapmyMap) {

                            this.myMap = myMap;

                   }

 

                   @Override

                   public String toString() {

                            return"name:" + username + ",pass:" + password;

                   }

 

                   public Container getXmls() {

                            if (xmls == null) {

                                     // 获取XML文件的内容

                                     System.out.println("path:"

                                                        +getClass().getResource("./ffm.xml").getPath());

                                     URL url =getClass().getResource("./ffm.xml");

                                     // 将XML的内容绑定到companys对象

                                     xmls = newDocumentContainer(url);

                            }

                            return xmls;

                   }

         }

}

运行结果如下:

利用JXPathContext获取属性 username:范芳铭

读取不存在的属性:null

path:/D:/develop/eclipse/Workspaces/test_all/bin/test/ffm83/commons/jxpath/ffm.xml

Map属性id:5

Map属性key:ffm

path:/D:/develop/eclipse/Workspaces/test_all/bin/test/ffm83/commons/jxpath/ffm.xml

公司名称:中国电信

员工工号:31000696

公司名称:102

 

[注] XPath访问数组或者集合时,数组或者集合的下标是从1开始,这点和java语言中规定的从0开始有点不同。

 

apache commons JXPath 说明,使用JXPath访问java对象、集合和XML文件