jaxb在unmarshal之后返回null vaue

时间:2016-12-29 04:48:03

标签: java xml jaxb

我正在尝试解组一个xml,它返回null值 XML:

<letterOutHeader>
        <lettertype>
            MN13
        </lettertype>
        <letterReqid>
            9294678
        </letterReqid>
        <language>
            en
        </language>
        <attentionTo></attentionTo>
        <addressLine1></addressLine1>
        <addressLine2></addressLine2>
        <city>Case City</city>
        <state>NY</state>
        <zipCode>59559</zipCode>
        <dateOfLetter>12/28/2016</dateOfLetter>
        <respondByDate/>
        <externalNum>Ac0287356894754</externalNum>
        <letterOutFlexField>
            <name>fieldOne </name>
            <value>valueOne</value>
        </letterOutFlexField>
        <letterOutFlexField>
            <name>fieldTwo</name>
            <value>valueTwo</value>
        </letterOutFlexField>
        <letterOutFlexField>
            <name>fieldThree</name>
            <value>valueThree</value>
        </letterOutFlexField>
        <letterOutFlexField>
            <name>fieldFour</name>
            <value>valueFour</value>
        </letterOutFlexField>
    </letterOutHeader>

豆:

package jaxb.Bean;

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class LetterOutHeader {
    String lettertype;
    String letterReqid;
    String language;
    String attentionTo;
    String addressLine1;
    String addressLine2;
    String city;
    String state;
    String zipCode;
    String dateOfLetter;
    String respondByDate;
    String externalNum;
    List<LetterOutFlexFieldBean> flexFields;
    @XmlAttribute 
    public String getLettertype() {
        return lettertype;
    }
    public void setLettertype(String lettertype) {
        this.lettertype = lettertype;
    }
    @XmlAttribute 
    public String getLetterReqid() {
        return letterReqid;
    }
    public void setLetterReqid(String letterReqid) {
        this.letterReqid = letterReqid;
    }
    @XmlAttribute 
    public String getLanguage() {
        return language;
    }
    public void setLanguage(String language) {
        this.language = language;
    }
    @XmlAttribute 
    public String getAttentionTo() {
        return attentionTo;
    }
    public void setAttentionTo(String attentionTo) {
        this.attentionTo = attentionTo;
    }
    @XmlAttribute 
    public String getAddressLine1() {
        return addressLine1;
    }
    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }
    @XmlAttribute 
    public String getAddressLine2() {
        return addressLine2;
    }
    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }
    @XmlAttribute
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    @XmlAttribute 
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    @XmlAttribute 
    public String getZipCode() {
        return zipCode;
    }
    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }
    @XmlAttribute 
    public String getDateOfLetter() {
        return dateOfLetter;
    }
    public void setDateOfLetter(String dateOfLetter) {
        this.dateOfLetter = dateOfLetter;
    }
    @XmlAttribute 
    public String getRespondByDate() {
        return respondByDate;
    }
    public void setRespondByDate(String respondByDate) {
        this.respondByDate = respondByDate;
    }
    @XmlAttribute 
    public String getExternalNum() {
        return externalNum;
    }
    public void setExternalNum(String externalNum) {
        this.externalNum = externalNum;
    }
    @XmlElement
    public List<LetterOutFlexFieldBean> getFlexFields() {
        return flexFields;
    }
    public void setFlexFields(List<LetterOutFlexFieldBean> flexFields) {
        this.flexFields = flexFields;
    }


}

UnMarshaling:

package jaxb.client;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import jaxb.Bean.LetterOutBean;
import jaxb.Bean.LetterOutHeader;

public class XmlToObject {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {  

            File file = new File("C:/Users/sindhu/Desktop/Kranthi/jaxB/baseXML.xml");  
            JAXBContext jaxbContext = JAXBContext.newInstance(LetterOutHeader.class);  

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
            LetterOutHeader que= (LetterOutHeader) jaxbUnmarshaller.unmarshal(file);  

            System.out.println(que.getCity());  
           /* System.out.println("Answers:");  
            List<Answer> list=que.getAnswers();  
            for(Answer ans:list)  
              System.out.println(ans.getId()+" "+ans.getAnswername()+"  "+ans.getPostedby());  
*/     
          } catch (JAXBException e) {  
            e.printStackTrace();  
          }  
    }

}

我已查看以下帖子以避免重复提问 JAXB unmarshal returning null values JAXB unmarshalling returning Null

1 个答案:

答案 0 :(得分:0)

您提供的XML与JAXB类之间存在许多不匹配。  首先,示例XML中没有属性,但JAXB包含@XmlAttribute。  其次,最好明确声明@XmlAccessorType,否则只查找public字段和方法。

<强> LetterOutHeader.java

package int1.d3;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="letterOutHeader")
@XmlAccessorType(XmlAccessType.FIELD)
public class LetterOutHeader {
    String lettertype;
    String letterReqid;
    String language;
    String attentionTo;
    String addressLine1;
    String addressLine2;
    String city;
    String state;
    String zipCode;
    String dateOfLetter;
    String respondByDate;
    String externalNum;
    @XmlElement(name="letterOutFlexField")
    List<LetterOutFlexFieldBean> flexField;


    public String getLettertype() {
        return lettertype;
    }
    public void setLettertype(String lettertype) {
        this.lettertype = lettertype;
    }

    public String getLetterReqid() {
        return letterReqid;
    }
    public void setLetterReqid(String letterReqid) {
        this.letterReqid = letterReqid;
    }

    public String getLanguage() {
        return language;
    }
    public void setLanguage(String language) {
        this.language = language;
    }

    public String getAttentionTo() {
        return attentionTo;
    }
    public void setAttentionTo(String attentionTo) {
        this.attentionTo = attentionTo;
    }

    public String getAddressLine1() {
        return addressLine1;
    }
    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

    public String getAddressLine2() {
        return addressLine2;
    }
    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }

    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }

    public String getZipCode() {
        return zipCode;
    }
    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    public String getDateOfLetter() {
        return dateOfLetter;
    }
    public void setDateOfLetter(String dateOfLetter) {
        this.dateOfLetter = dateOfLetter;
    }

    public String getRespondByDate() {
        return respondByDate;
    }
    public void setRespondByDate(String respondByDate) {
        this.respondByDate = respondByDate;
    }

    public String getExternalNum() {
        return externalNum;
    }
    public void setExternalNum(String externalNum) {
        this.externalNum = externalNum;
    }
    public List<LetterOutFlexFieldBean> getFlexFields() {
        if(flexField == null) {
            flexField = new ArrayList<LetterOutFlexFieldBean>();
        }
        return this.flexField;
    }
}