将XPath转换为JXPath

时间:2016-01-20 14:07:56

标签: xpath jxpath

我有以下XML:

 <?xml version="1.0" encoding="UTF-8"?>
        <CreateReservation>
           <NewReservation>
              <Profile op="D">
                 <ProfileID>ID</ProfileID>
                 <ProfileType>TYPE</ProfileType>
              </Profile>
              <Number>123456</Number>
           </NewReservation>

通过以下方式与来自CreateReservation类的JAXB进行编组:

CreateReservation request = new CreateReservation("123456", "D");
String xpathExpr = "boolean(//*[local-name()='CreateReservationRQ']//@op='D')"
JAXBContext context = JAXBContext.newInstance(CreateReservation.class);
marshaller = context.createMarshaller();    
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
marshaller.marshal(request, document); //line creates xml presented above

//EVALUATION
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.evaluate(xpathExpr, document, XPathConstants.BOOLEAN); //line evaluates xpath and returns true

总结每一个,提供代码组合CreateReservation类,从中创建相应的XML,并使用xpath表达式检查创建的xml是否有任何<CreateReservation>节点,其中有{{1}的任何子节点}属性。使用以下xpath表达式完成检查:

op="D"

条件正常,fot提供代码示例。现在我想摆脱boolean(//*[local-name()='CreateReservationRQ']//@op='D') 类的不必要的编组。为此,我想从JAXB切换到JXPath:

CreateReservation

PathContext jXPathContext = JXPathContext.newContext(obj); Object obj = jXPathContext.getValue(CONDITION); if (obj != null) { //code should behave exactly the same, as my previous model } - obj类的成员(它的结构与上面xml中的结构完全相同)

CreateReservation - 我的JXPath查询

这个想法是,那一行:

CONDITION

Object obj = jXPathContext.getValue(CONDITION); 类的CreateREservation字段等于&#39; D&#39;时,返回任何节点。如果没有节点,则返回null。功能代码应该与以前完全相同。你能告诉我op应该是什么样的吗?

1 个答案:

答案 0 :(得分:0)

对于动态xpath到对象的实现,你可以看一下。

示例1:JavaBean属性访问

JXPath可用于访问JavaBean的属性。

 public class Employee {
    public String getFirstName(){
       ...
    }
 }

 Employee emp = new Employee();
 JXPathContext context = JXPathContext.newContext(emp);
 String fName = (String)context.getValue("firstName");

在这个例子中,我们使用JXPath来访问emp bean的属性。在这个简单的例子中,调用JXPath等同于在bean上调用getFirstName()。

示例2:嵌套Bean属性访问

JXPath可以遍历对象图:

 public class Employee {
    public Address getHomeAddress(){
       ...
    }
 }
 public class Address {
    public String getStreetNumber(){
       ...
    }
 }

 Employee emp = new Employee();
 ...

 JXPathContext context = JXPathContext.newContext(emp);
 String sNumber = (String)context.getValue("homeAddress/streetNumber");

在这种情况下,XPath用于访问嵌套bean的属性。 由xpath标识的属性不必是“叶子”属性。例如,我们可以在上面的例子中提取整个Address对象:

Address addr = (Address)context.getValue("homeAddress");

示例3:设置属性

JXPath可用于修改属性值。

 public class Employee {
    public Address getAddress() {
       ...
    }

    public void setAddress(Address address) {
       ...
    }
 }

 Employee emp = new Employee();
 Address addr = new Address();
 ...

 JXPathContext context = JXPathContext.newContext(emp);
 context.setValue("address", addr);
 context.setValue("address/zipCode", "90190");

示例4:创建对象 JXPath可用于创建新对象。首先,创建AbstractFactory的子类并将其安装在JXPathContext上。然后调用createPathAndSetValue()而不是“setValue”。当JXPathContext发现路径的中间节点为空时,它将调用您的AbstractFactory。它不会覆盖现有节点。

public class AddressFactory extends AbstractFactory {
    public boolean createObject(JXPathContext context,
               Pointer pointer, Object parent, String name, int index){
     if ((parent instanceof Employee) && name.equals("address"){
       ((Employee)parent).setAddress(new Address());
       return true;
     }
     return false;
   }
 }

 JXPathContext context = JXPathContext.newContext(emp);
 context.setFactory(new AddressFactory());
 context.createPathAndSetValue("address/zipCode", "90190");

有关详情帮助,请查看以下链接:JxPath Tutorial