我有一个xml文件:
@XmlRootElement(name = "Data", namespace = "http://vv.com/namespace")
public class AddressDto implements Serializable
{
private String street;
private String city;
private CountryDto country;
public AddressDto()
{
super();
}
@XmlElement(name = "Addr")
public String getStreet1()
{
return street;
}
public void setStreet1(final String street1)
{
this.street = street1;
}
@XmlElement(name = "Locality")
public String getCity()
{
return city;
}
public void setCity(final String city)
{
this.city = city;
}
public CountryDto getCountry()
{
return country;
}
public void setCountry(final CountryDto country)
{
this.country = country;
}
}
AddressDto.java
public class CountryDto implements Serializable
{
private String name;
private String code;
public CountryDto()
{
super();
}
@XmlElement(name = "CountryCode")
public String getCode()
{
return code;
}
public void setCode(final String code)
{
this.code = code;
}
@XmlElement(name = "Country")
public String getName()
{
return name;
}
public void setName(final String name)
{
this.name = name;
}
}
CountryDto档案:
JAXBContext context = JAXBContext.newInstance(AddressDto.class);
Unmarshaller un = context.createUnmarshaller();
AddressDto emp = (AddressDto) un.unmarshal(response.getSOAPBody().extractContentAsDocument());
return emp;
当我运行代码时
{{1}}
我可以在AddressDto对象中获取街道和城市。但是田野国家显示为空......任何想法在这里出错了吗?
谢谢
答案 0 :(得分:0)
Parser不知道Country / CountryCode的含义。您在AddressDto类中仅将Addr和Locality定义为XmlElement。因此,您将获得null。
您需要将xml修改为以下内容:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:Data xmlns:ns1="http://vv.com/namespace">
<Addr>Address 1</Addr1>
<Locality>San Francisco</Locality>
<CountryDto>
<Country>Japan</Country>
<CountryCode>JP</CountryCode>
</CountryDto>
</ns1:Data>
</soapenv:Body>
</soapenv:Envelope>
并修改你的地址以便 getCountry()也是XmlElement:
@XmlElement(name = "CountryDto")
public CountryDto getCountry()
{
return country;
}
现在Country和CountryCode不再为null