【问题标题】:How to use XPATH functions(concat, string-join) in JDOM2?如何在 JDOM2 中使用 XPATH 函数(concat、string-join)?
【发布时间】:2014-06-20 05:01:22
【问题描述】:

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

【问题讨论】:

    标签: xpath jdom-2


    【解决方案1】:

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

    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,您也会遇到与 nameExpression 相同的返回类型问题。 987654331@ 返回一个字符串而不是一个元素)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多