以下代码摘自Spring-ws手册:
public class HolidayEndpoint {
private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas";
private XPath startDateExpression;
private XPath endDateExpression;
private XPath nameExpression;
private HumanResourceService humanResourceService;
@Autowired
public HolidayEndpoint(HumanResourceService humanResourceService) (2)
throws JDOMException {
this.humanResourceService = humanResourceService;
Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
startDateExpression = XPath.newInstance("//hr:StartDate");
startDateExpression.addNamespace(namespace);
endDateExpression = XPath.newInstance("//hr:EndDate");
endDateExpression.addNamespace(namespace);
nameExpression = XPath.newInstance("concat(//hr:FirstName,' ',//hr:LastName)");
nameExpression.addNamespace(namespace);
}我的问题是,这似乎是使用JDOM 1.0,而我想使用JDOM 2.0。
如何将此代码从JDOM 1.0转换为JDOM 2.0?为什么spring还没有更新他们的示例代码呢?
谢谢!
发布于 2012-08-14 03:38:29
JDOM2仍然是相对较新的……但是,JDOM1.x中的XPath工厂尤其糟糕……JDOM 2.x为它提供了一个新的api。旧API的存在是为了向后兼容/迁移。请看这里的文档,了解一些原因,以及new API in JDOM 2.x。
在您的示例中,您可能希望将代码替换为以下内容:
XPathExpression<Element> startDateExpression =
XPathFactory.instance().compile("//hr:StartDate", Filters.element(), null, namespace);
List<Element> startdates = startDateExpression.evaluate(mydocument);罗尔夫
发布于 2013-07-12 02:07:38
为了使用上面来自Rolf的代码来解析值,遍历列表或获取列表中的第一个元素,假设只有一个元素。
List<Element> startdates = startDateExpression.evaluate(mydocument);
for (Element e: startdates){
logger.debug("value= " + e.getValue());
}或
List<Element> startdates = startDateExpression.evaluate(mydocument);
logger.debug("value " + startdates.get(0).getValue();https://stackoverflow.com/questions/11940708
复制相似问题