EclipseLink Moxy unmarshall and getValueByXPath gives null

白昼怎懂夜的黑 提交于 2019-12-11 08:05:09

问题


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.

Do I need to specify any NameSpaceResolver?

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

My code:

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

My XML:

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

         </SendingCustomer>
         </Transaction>

回答1:


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

<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


来源:https://stackoverflow.com/questions/17687460/eclipselink-moxy-unmarshall-and-getvaluebyxpath-gives-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!