EclipseLink Moxy unmarshall和getValueByXPath给出null [英] EclipseLink Moxy unmarshall and getValueByXPath gives null

查看:49
本文介绍了EclipseLink Moxy unmarshall和getValueByXPath给出null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来获取unmarshall并通过Xpath查询unmarshelled对象。
我可以在解组之后获取对象,但是在通过XPath查询时,该值将变为空。

I use the below code to get unmarshall and query the unmarshelled object by Xpath. I am able to get the object after unmarshalling, but while querying by XPath, the value is coming as null.

我是否需要指定任何NameSpaceResolver?

Do I need to specify any NameSpaceResolver?

如果您正在寻找更多信息,请与我们联系。

Please let me know if you are looking for any further information.

我的代码:

         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>


推荐答案

由于您的示例中没有名称空间,因此您不需要需要担心利用 NamespaceResolver 。你没有提供你遇到麻烦的XPath,所以我刚刚在下面的例子中选择了一个。

Since there are no namespaces in your example you do not need to worry about leveraging a NamespaceResolver. You didn't provide the XPath that you were having trouble with, so I have just picked one in the example below.

JAVA MODEL

交易

Transaction

import javax.xml.bind.annotation.*;

@XmlRootElement(name="Transaction")
public class Transaction {

    @XmlElement(name="SendingCustomer")
    private Customer sendingCustomer;

}

客户

Customer

import javax.xml.bind.annotation.XmlAttribute;

public class Customer {

    @XmlAttribute
    private String firstName;

    @XmlAttribute
    private String lastNameDecrypted;

    @XmlAttribute(name="OnWUTrustList")
    private boolean onWUTrustList;

    @XmlAttribute(name="WUTrustListType")
    private String wuTrustListType;

}

DEMO CODE

input.xml

input.xml

<Transaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SendingCustomer firstName="test" lastNameDecrypted="SMITH"
        OnWUTrustList="false" WUTrustListType="NONE">

    </SendingCustomer>
</Transaction>

演示

Demo

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

}

输出

Output

test

这篇关于EclipseLink Moxy unmarshall和getValueByXPath给出null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆