문제

Here I have a simple project. 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 put in the root folder of project:

<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>

Source Code to decode:

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

But both the nameExpression and nameExpression2 don't work. The output result is:

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 causes compilation error. Could you please help. Thanks.

도움이 되었습니까?

해결책

For nameExpression the expression you're using will return a result of type string rather than a node set, so Filters.element() doesn't match and therefore nameExpression.evaluateFirst will return null. You should declare it as an XPathExpression<String> instead, and use an appropriate filter.

XPathExpression<String> nameExpression;

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

As for nameExpression2, the string-join function is an XPath 2.0 feature but Jaxen only supports XPath 1.0 (and even if you had XPath 2.0 support you'd hit the same return type problem as with nameExpression as string-join returns a string rather than an element).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top