如何在JDOM2中使用XPATH函数(concat,string-join)?

时间:2014-05-03 15:22:10

标签: xpath jdom-2

这里我有一个简单的项目。 的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jeecourse.tutorial</groupId>
<artifactId>JDOM2Demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>JDOM2Demo</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>org.jdom</groupId>
        <artifactId>jdom</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
        <version>1.1.6</version>
    </dependency>
</dependencies>
</project>

hr.xml放在项目的根文件夹中:

<HolidayRequest xmlns="http://mycompany.com/hr/schemas">
<Holiday>
    <StartDate>2006-07-03</StartDate>
    <EndDate>2006-07-07</EndDate>
</Holiday>
<Employee>
    <Number>42</Number>
    <FirstName>Arjen</FirstName>
    <LastName>Poutsma</LastName>
</Employee>
</HolidayRequest>

要解码的源代码:

package com.jeecourse.tutorial;

import java.io.IOException;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;

public class XPathDecode {

private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas";
public static void main(String args[]) {

    XPathExpression<Element> startDateExpression;
    XPathExpression<Element> endDateExpression;
    XPathExpression<Element> nameExpression;
    XPathExpression<Element> nameExpression2;
    XPathExpression<Element> fnameExpression;
    XPathExpression<Element> lnameExpression;
    XPathFactory xFactory;


    SAXBuilder sax = new SAXBuilder();
    Document holidayRequest = null;
    try {
        holidayRequest = sax.build("hr.xml");
    } catch (JDOMException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);

    // use the default implementation
    xFactory = XPathFactory.instance();
    startDateExpression = xFactory.compile("//hr:StartDate", Filters.element(), null, namespace);
    endDateExpression = xFactory.compile("//hr:EndDate", Filters.element(), null, namespace);
    fnameExpression = xFactory.compile("//hr:FirstName", Filters.element(), null, namespace);
    lnameExpression = xFactory.compile("//hr:LastName", Filters.element(), null, namespace);
    nameExpression = xFactory.compile("concat(//hr:FirstName,'#',//hr:LastName)", Filters.element(), null, namespace);
    //nameExpression2 = xFactory.compile("string-join((//hr:FirstName, //hr:LastName), '#')", Filters.element(), null, namespace);

    Element startDate = startDateExpression.evaluateFirst(holidayRequest);
    System.out.println(startDate.getValue());

    Element endDate = endDateExpression.evaluateFirst(holidayRequest);
    System.out.println(endDate.getValue());

    Element fname = fnameExpression.evaluateFirst(holidayRequest);
    System.out.println(fname.getValue());

    Element lname = lnameExpression.evaluateFirst(holidayRequest);
    System.out.println(lname.getValue());

    Element name = nameExpression.evaluateFirst(holidayRequest);
    System.out.println(name.getValue());

    //Element name2 = nameExpression2.evaluateFirst(holidayRequest);
    System.out.println(name2.getValue());
}
}

但nameExpression和nameExpression2都不起作用。输出结果为:

2006-07-03
2006-07-07
Arjen
Poutsma
Exception in thread "main" java.lang.NullPointerException
at com.jeecourse.tutorial.XPathDecode.main(XPathDecode.java:62)

nameExpression2导致编译错误。能否请你帮忙。感谢。

1 个答案:

答案 0 :(得分:2)

对于nameExpression,您使用的表达式将返回string类型的结果而不是节点集,因此Filters.element()不匹配,因此nameExpression.evaluateFirst将返回null。您应该将其声明为XPathExpression<String>,并使用适当的过滤器。

XPathExpression<String> nameExpression;

nameExpression = xFactory.compile("concat(//hr:FirstName,'#',//hr:LastName)",
      Filters.fstring(), null, namespace);

至于nameExpression2string-join函数是一个XPath 2.0特性,但Jaxen只支持XPath 1.0(即使你有XPath 2.0支持,你也会遇到与{相同的返回类型问题) {1}} nameExpression返回字符串而不是元素。