xpath - EclipseLink Moxy unmarshall 和 getValueByXPath 给出 null

标签 xpath jaxb eclipselink moxy

我使用下面的代码来解码​​并通过 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>

最佳答案

由于您的示例中没有命名空间,因此您无需担心使用 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;

}

演示代码

输入.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

关于xpath - EclipseLink Moxy unmarshall 和 getValueByXPath 给出 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17687460/

相关文章:

java - 替代 JPA 提供商(例如 eclipselink)? eclipselink 环境是否特定?

c# - 通用 Windows 平台中的 System.Xml.XPath

xpath:破译这个 xpath?

java - 如何通过 XPath 从同一父节点内的文本节点中提取文本

java - 使用 Spring MVC 的 JAXB 注释

Java JPA (EclipseLink) 如何在保留实际实体之前接收下一个 GeneratedValue?

xml - XSLT - 将前一个兄弟的元素与当前的节点元素进行比较

java - 在 Ant 中从 JAXB 类文件生成 XML 模式

java - 使用 JAXB marshal 添加/修改 XML 文件中的元素

java - 启用 JPA 静态编织(eclipselink)