Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
EclipseLink Moxy解组和getValueByXPath提供空值_Xpath_Jaxb_Eclipselink_Moxy - Fatal编程技术网

EclipseLink Moxy解组和getValueByXPath提供空值

EclipseLink Moxy解组和getValueByXPath提供空值,xpath,jaxb,eclipselink,moxy,Xpath,Jaxb,Eclipselink,Moxy,我使用下面的代码获取解组并通过Xpath查询解组对象。 我可以在解组后获取对象,但在通过XPath查询时,值为null 是否需要指定任何名称空间解析程序 如果您想了解更多信息,请告诉我 我的代码: JAXBContext jaxbContext = (JAXBContext) JAXBContextFactory.createContext(new Class[] {Transaction.class}, null); Unmarshaller unmarsh

我使用下面的代码获取解组并通过Xpath查询解组对象。 我可以在解组后获取对象,但在通过XPath查询时,值为null

是否需要指定任何名称空间解析程序

如果您想了解更多信息,请告诉我

我的代码:

         JAXBContext jaxbContext = (JAXBContext) JAXBContextFactory.createContext(new Class[] {Transaction.class}, null);
         Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
         StreamSource streamSource= new StreamSource(new StringReader(transactionXML));
         transaction = unmarshaller.unmarshal(streamSource, Transaction.class).getValue();
         String displayValue = jaxbContext.getValueByXPath(transaction, xPath, null, String.class);
我的XML:

         <Transaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                          xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
         <SendingCustomer firstName="test">

         </SendingCustomer>
         </Transaction>

由于您的示例中没有名称空间,您不必担心如何利用
名称空间解析器。您没有提供遇到问题的XPath,因此我在下面的示例中选择了一个

JAVA模型

交易

import javax.xml.bind.annotation.*;
@XmlRootElement(name=“事务”)
公共类事务{
@XmlElement(name=“SendingCustomer”)
私人客户发送客户;
}
客户

import javax.xml.bind.annotation.xmldattribute;
公共类客户{
@XmlAttribute
私有字符串名;
@XmlAttribute
私有字符串lastNameDecrypted;
@XmlAttribute(name=“OnWUTrustList”)
私有布尔信任列表;
@XmlAttribute(name=“WUTrustListType”)
私有字符串类型;
}
演示代码

input.xml


演示

输出


那么您的XPath表达式是什么?“值为null”-字符串是null(未设置)还是空的?谢谢Blaise。这很有效。我给了/Transaction/SendingCustomer/@firstName而不是SendingCustomer/@firstName。
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContext;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jaxbContext = (JAXBContext) JAXBContextFactory.createContext(new Class[] {Transaction.class}, null);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        StreamSource streamSource= new StreamSource("src/forum17687460/input.xml");
        Transaction transaction = unmarshaller.unmarshal(streamSource, Transaction.class).getValue();
        String displayValue = jaxbContext.getValueByXPath(transaction, "SendingCustomer/@firstName", null, String.class);
        System.out.println(displayValue);
    }

}
test