1

这里我有一个简单的项目。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 导致编译错误。能否请你帮忙。谢谢。

4

1 回答 1

2

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

XPathExpression<String> nameExpression;

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

至于nameExpression2,该string-join函数是 XPath 2.0 功能,但 Jaxen 仅支持 XPath 1.0(即使您支持 XPath 2.0,您也会遇到与nameExpressionasstring-join返回字符串而不是元素相同的返回类型问题)。

于 2014-05-03T16:58:51.203 回答