我有以下xml响应:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:DealLookupResponse xmlns:ns2="http://www.starstandards.org/webservices/2005/10/transport" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:anyType">
<Deal>
<CompanyNumber>CN7</CompanyNumber>
<DealNumber>111</DealNumber>
<RecordStatus>A</RecordStatus>
<SalesPersons>
<SalesPerson>
<RecordType/>
<SalesPersonID>CL1</SalesPersonID>
<Status>A</Status>
<SaleDate>20140806</SaleDate>
<SalesPersonName>CLOSER 1</SalesPersonName>
<IncentiveCommission>0.00</IncentiveCommission>
<TotalCommission>0.00</TotalCommission>
</SalesPerson>
</SalesPersons>
</Deal>
</ns2:DealLookupResponse>
当我将这个xml解组为Java Object时,我获得了Deal对象的所有值,但SalesPersons在列表中有一个元素,并且该元素的所有属性都为null。我错过了什么?
我的DealLookupResponse类是
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"deal"
})
@XmlRootElement(name = "DealLookupResponse", namespace="http://www.starstandards.org/webservices/2005/10/transport")
public class DealLookupResponse {
@XmlElement(name = "Deal", required = false)
Deal deal;
public Deal getDeal() {
return deal;
}
public void setDeal(Deal deal) {
this.deal = deal;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Deal", propOrder = {
"companyNumber",
"dealNumber",
"recordStatus",
"salesPersons"
}, namespace = "http://www.starstandards.org/webservices/2005/10/transport")
public class Deal {
@XmlElement(name="CompanyNumber")
String companyNumber;
@XmlElement(name="DealNumber")
String dealNumber;
@XmlElement(name="RecordStatus")
String recordStatus;
@XmlElementWrapper
@XmlElement(name="SalesPerson")
List<SalesPerson> salesPersons;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SalesPerson", propOrder = {
"recordType",
"salesPersonID",
"status",
"saleDate",
"salesPersonName",
"incentiveCommission",
"totalCommission"
}, namespace = "http://www.starstandards.org/webservices/2005/10/transport")
public class SalesPerson {
@XmlElement(name="RecordType")
String recordType;
@XmlElement(name="SalesPersonID")
Long salesPersonID;
@XmlElement(name="Status")
String status;
@XmlElement(name="SaleDate")
Date saleDate;
@XmlElement(name="SalesPersonName")
String salesPersonName;
@XmlElement(name="IncentiveCommission")
Double incentiveCommission;
@XmlElement(name="TotalCommission")
Double totalCommission;
}
我的解组代码如下所示:
JAXBContext jc = JAXBContext.newInstance(DealLookupResponse.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
DealLookupResponse je = (DealLookupResponse)unmarshaller.unmarshal(xmlInputSource);
答案 0 :(得分:2)
由于您未在name
注释中指定@XmlElementWrapper
,因此您将获得默认行为。
@XmlElementWrapper
@XmlElement(name="SalesPerson")
List<SalesPerson> salesPersons;
这意味着它将对应于以下XML:
<salesPersons>
<SalesPerson>
</SalesPerson>
<salesPersons>
您需要将@XmlElementWrapper
字段上的salesPersons
注释更改为以下内容:
@XmlElementWrapper(name="SalesPersons")
@XmlElement(name="SalesPerson")
List<SalesPerson> salesPersons;
当您遇到解组问题时,请填充对象模型,然后将其封送到XML。然后将此XML与您要解组的XML进行比较,以查看是否存在任何差异。
答案 1 :(得分:0)
使用此XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DealLookupResponse xmlns="http://www.starstandards.org/webservices/2005/10/transport" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:anyType">
<Deal>
<CompanyNumber>CN7</CompanyNumber>
<DealNumber>111</DealNumber>
<RecordStatus>A</RecordStatus>
<SalesPersons>
<SalesPerson>
<RecordType/>
<SalesPersonID>CL1</SalesPersonID>
<Status>A</Status>
<SaleDate>20140806</SaleDate>
<SalesPersonName>CLOSER 1</SalesPersonName>
<IncentiveCommission>0.00</IncentiveCommission>
<TotalCommission>0.00</TotalCommission>
</SalesPerson>
</SalesPersons>
</Deal>
</DealLookupResponse>
及以下课程
<强> DealLookupResponse 强>
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DealLookupResponse", propOrder = {
"Deal"
})
@XmlRootElement(name = "DealLookupResponse", namespace="http://www.starstandards.org/webservices/2005/10/transport")
public class DealLookupResponse {
@XmlElement(required = true)
Deal Deal;
}
<强>交易强>
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"CompanyNumber",
"DealNumber",
"RecordStatus",
"SalesPersons"
})
@XmlRootElement(name = "Deal", namespace="http://www.starstandards.org/webservices/2005/10/transport")
public class Deal {
@XmlElement(required = true)
String CompanyNumber;
@XmlElement(required = true)
String DealNumber;
@XmlElement(required = true)
String RecordStatus;
@XmlElementWrapper
@XmlElement(required = true)
List<SalesPerson> SalesPersons;
}
<强>业务员强>
@XmlType(name = "SalesPerson", propOrder = {
"RecordType",
"RalesPersonID",
"Status",
"SaleDate",
"SalesPersonName",
"IncentiveCommission",
"TotalCommission"
}, namespace = "http://www.starstandards.org/webservices/2005/10/transport")
public class SalesPerson {
@XmlElement(name="RecordType")
String RecordType;
@XmlElement(name="SalesPersonID")
Long RalesPersonID;
@XmlElement(name="Status")
String Status;
@XmlElement(name="SaleDate")
Date SaleDate;
@XmlElement(name="SalesPersonName")
String SalesPersonName;
@XmlElement(name="IncentiveCommission")
Double IncentiveCommission;
@XmlElement(name="TotalCommission")
Double TotalCommission;
}