【问题标题】:EclipseLink Moxy unmarshall and getValueByXPath gives nullEclipseLink Moxy unmarshall 和 getValueByXPath 给出 null
【发布时间】:2013-07-16 21:37:37
【问题描述】:

我使用下面的代码来解组​​并通过 Xpath 查询解组的对象。 我能够在解组后获取对象,但是在通过 XPath 查询时,该值变为 null。

我需要指定任何 NameSpaceResolver 吗?

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

我的代码:

         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 表达式是什么? “该值为 null” - 字符串为 null(未设置)还是为空?

标签: xpath jaxb eclipselink moxy


【解决方案1】:

由于您的示例中没有命名空间,您无需担心使用NamespaceResolver。您没有提供您遇到问题的 XPath,所以我只在下面的示例中选择了一个。

JAVA 模型

交易

import javax.xml.bind.annotation.*;

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

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

}

客户

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;

}

演示代码

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>

演示

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

【讨论】:

  • 谢谢 Blaise。这很好。我给了 /Transaction/SendingCustomer/@firstName 而不是 SendingCustomer/@firstName 。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 2012-01-14
  • 2014-12-23
  • 1970-01-01
相关资源
最近更新 更多