JDOM 2 and xpath
Asked Answered
F

2

5

Here is the following code excerpted from the Spring-ws manual:

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);
  }

My problem is that this appears to be using JDOM 1.0 and I'd like to use JDOM 2.0.

How do I convert this code from JDOM 1.0 to JDOM 2.0? Why hasn't spring updated their sample code?

Thanks!

Fatal answered 13/8, 2012 at 19:9 Comment(1)
Have you updated your imports to use jDom2? For example, the import for Element is import org.jdom2.Element;.Conic
X
7

JDOM2 is still relatively new.... but, the XPath factory in JDOM 1.x is particularly broken... and JDOM 2.x has a new api for it. The old API exists for backward compatibility/migration. Have a look at this document here for some reasoning, and the new API in JDOM 2.x.

In your case, you probably want to replace the code with something like:

XPathExpression<Element> startDateExpression = 
    XPathFactory.instance().compile("//hr:StartDate", Filters.element(), null, namespace);

List<Element> startdates = startDateExpression.evaluate(mydocument);

Rolf

Xylene answered 13/8, 2012 at 19:38 Comment(3)
once I've done that, how do I use the xPathExpression to retrieve the value? the valueOf method is gone.Fatal
The XPathExpression has the evaluate and EvaluateFirst which respectively return a list of all nodes matching the expression, or just the first matching node. in the example above, startdates is a list containing all the StartDate Elements in the document.Xylene
Thanks again for all of your help. Now I'm lost, though. I'm working through static.springsource.org/spring-ws/sites/2.0/reference/html/… on chapter 3. I'm trying to figure out how to convert the code there to use JDOM 2.Fatal
O
0

In order to parse the value using the code above from Rolf, iterate through the list or get the first element from the List assuming there's only one.

List<Element> startdates = startDateExpression.evaluate(mydocument);

    for (Element e: startdates){
        logger.debug("value= " + e.getValue());
    }

or

List<Element> startdates = startDateExpression.evaluate(mydocument);
logger.debug("value " + startdates.get(0).getValue();
Oswaldooswalt answered 11/7, 2013 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.