0

我有以下 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>节点,该节点具有任何具有op="D"属性的子节点。使用以下 xpath 表达式完成检查:

boolean(//*[local-name()='CreateReservationRQ']//@op='D')

条件工作正常,提供代码示例。现在我想摆脱不必要的CreateReservation班级编组。为此,我想从 JAXB 切换到 JXPath:

PathContext jXPathContext = JXPathContext.newContext(obj);
Object obj = jXPathContext.getValue(CONDITION);
if (obj != null) {
   //code should behave exactly the same, as my previous model
}

obj-CreateReservation类的成员(它的结构与上面的 xml 中呈现的完全相同)

CONDITION- 我的 JXPath 查询

这个想法是,那行:

  Object obj = jXPathContext.getValue(CONDITION);

CreateREservation当类的op字段等于“D”时,返回任何节点。如果不存在节点,则返回 null。功能代码的行为应该与以前完全相同。你能告诉我我CONDITION应该是什么样子吗?

4

1 回答 1

1

对于对象实现的动态 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 教程

于 2016-02-15T05:08:47.940 回答