JAXB复杂XML反序列化(解组)

时间:2015-07-14 21:06:07

标签: java xml jaxb unmarshalling

我正在尝试将xml数据反序列化为Java对象。它反序列化但是子列表是空的,即数组是空的。我尝试了不同的方式但失败了。

输入xml字符串

String tempXML = "<searchbadge xmlns=\"www.Ingo.com/Api/v1.0\"   
xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance  
\"><errorcode>0</errorcode><errormessage/><matches><match><address>road 
A</address><badgeno>10109</badgeno><bday>7/13/2015 12:00:00 AM</bday>
<city>Roswell</city><name>Jason Reed</name><phone>876 456 2345</phone>
<state>Georgia</state><zip>30320</zip></match></matches></searchbadge>";

Root Class

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "searchbadge",namespace="www.Ingo.com/Api/v1.0")

public class SearchBadgeResponse implements Serializable{
@XmlElement(name = "errorcode")
  private String errorcode ;

@XmlElement(name = "errormessage")
private String errormessage ;

@XmlElementWrapper(name = "matches")      
@XmlElement(name = "match")
private List<Match> matches ;

public String getErrorCode() {
    return errorcode;
}

public void setErrorCode(String errorcode) {
    this.errorcode = errorcode;
}
public String getErrorMessage() {
    return errormessage;
}

public void setErrorMessage(String errormessage) {
    this.errormessage = errormessage;
}
public List<Match> getMatches() {
    return this.matches;
}   
public void setMatches(List<Match> matches) {
    this.matches = matches;
  }
}

儿童班:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "match")

public class Match implements Serializable{

@XmlElement(name = "name")
public String name;

@XmlElement(name = "badgeno")
public String badgeno;

@XmlElement(name = "phone")
public String phone ;

@XmlElement(name = "bday")
public String bday;

@XmlElement(name = "address")
public String address;

@XmlElement(name = "city")
public String city ;

@XmlElement(name = "state")
public String state ;

@XmlElement(name = "zip")
public String zip ;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name ;
}

public String getBadgeNo() {
    return badgeno;
}
public void setBadgeNo(String badgeno) {
    this.badgeno = badgeno ;
}

public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone ;
}

public String getBDay() {
    return bday;
}
public void setBDay(String bday) {
    this.bday = bday ;
}

public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address ;
}

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 getZip() {
    return zip;
}
public void setzip(String zip) {
    this.zip = zip ;
}    
}

解组代码

public static  SearchBadgeResponse getObjectTmp(String valXML)  {
    SearchBadgeResponse myobj = null;

    try

    {
    JAXBContext jaxbContextDes =   
     JAXBContext.newInstance(SearchBadgeResponse.class);
    Unmarshaller unmarshaller = jaxbContextDes.createUnmarshaller();

    StringReader reader = new StringReader(valXML);
    myobj = (SearchBadgeResponse) unmarshaller.unmarshal(reader);

    return  myobj;
    }
    catch(JAXBException jex)
    {
        jex.printStackTrace();      
    }
    return myobj;
}

我无法弄清楚这个问题。任何提示/样本都很明显。

0 个答案:

没有答案