简体   繁体   中英

jaxb unmarshaller reading values as 0

JAXBContext jaxbContext = JAXBContext.newInstance(BatchwisePricingJob.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

StringReader reader = new StringReader(batchprice.toString());

BatchwisePricingJob batch = (BatchwisePricingJob) jaxbUnmarshaller.unmarshal(reader);
ArrayList<Price> pricingOfProduct = batch.getPricingOfProduct();

int i = 0;
for (Price price : pricingOfProduct) {
    i++;
    System.out.println("customer id:" + i + " " + price.getCustomerId());
    System.out.println("material id:" + i + " " + price.getMaterialId());
}

also given @XmlElement annotaion to getters/setters but then it throws the exception for Illegealannotationexception

Class has two properties of the same name "customerId"
    this problem is related to the following location:
        at public int com.efl.efms.batch.ws.data.batchwisePricing.Price.getCustomerId()
        at com.efl.efms.batch.ws.data.batchwisePricing.Price
        at private java.util.ArrayList com.efl.efms.batch.ws.data.batchwisePricing.BatchwisePricingJob.pricingOfProduct
        at com.efl.efms.batch.ws.data.batchwisePricing.BatchwisePricingJob
    this problem is related to the following location:
        at private int com.efl.efms.batch.ws.data.batchwisePricing.Price.customerId
        at com.efl.efms.batch.ws.data.batchwisePricing.Price
        at private java.util.ArrayList com.efl.efms.batch.ws.data.batchwisePricing.BatchwisePricingJob.pricingOfProduct
        at com.efl.efms.batch.ws.data.batchwisePricing.BatchwisePricingJob

The values being unmarshalled will be null if the XML document does not match your mappings. The easiest thing to do is populate your object model and then marshal it out to see the XML that corresponds to your current mappings. You can use @XmlElement and @XmlAttribute to specify name you want to map to if the desired one differs from the default.

You will see the following exception if you map both the field and its corresponding property. The following article will help you: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

Class has two properties of the same name "customerId"
    this problem is related to the following location:
        at public int com.efl.efms.batch.ws.data.batchwisePricing.Price.getCustomerId()
        at com.efl.efms.batch.ws.data.batchwisePricing.Price
        at private java.util.ArrayList com.efl.efms.batch.ws.data.batchwisePricing.BatchwisePricingJob.pricingOfProduct
        at com.efl.efms.batch.ws.data.batchwisePricing.BatchwisePricingJob
    this problem is related to the following location:
        at private int com.efl.efms.batch.ws.data.batchwisePricing.Price.customerId
        at com.efl.efms.batch.ws.data.batchwisePricing.Price
        at private java.util.ArrayList com.efl.efms.batch.ws.data.batchwisePricing.BatchwisePricingJob.pricingOfProduct
        at com.efl.efms.batch.ws.data.batchwisePricing.BatchwisePricingJob

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM