我得到了以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<CreateReservation>
<NewReservation>
<Profile op="D">
<ProfileID>ID</ProfileID>
<ProfileType>TYPE</ProfileType>
</Profile>
<Number>123456</Number>
</NewReservation>
使用JAXB从CreateReservation类封送的方式如下:
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总结每一个XML,给出代码封送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应该是什么样子吗?
发布于 2016-02-15 05:08:47
对于动态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的一个属性。在这个简单的例子中,对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教程
https://stackoverflow.com/questions/34902264
复制相似问题