大家好!我正在开发这个安静的Web服务客户端,但是我在尝试获取嵌套元素时遇到了问题。这是我收到的XML。
<InstalledProducts>
<InstalledProduct>
<uri>
<url>
http://immix-test.natinst.com/ib-rest/1.0/installed-product/NIC:458513.xml
</url>
<urn>urn:installed-product:NIC:458513</urn>
</uri>
<instanceId>458513</instanceId>
<site>NIC</site>
<serialNumber>A49AD1</serialNumber>
<productNumber>459513</productNumber>
<ibStatus>Latest</ibStatus>
<ibCreationDate>1998-10-14</ibCreationDate>
<partNumber>
<urn>urn:product-item:181830E-01</urn>
</partNumber>
<latestServiceContracts>
<url>
http://immix-test.natinst.com/ib-rest/1.0/installed-product/NIC:458513/latestServiceContracts.xml
</url>
</latestServiceContracts>
<allServiceContracts>
<url>
http://immix-test.natinst.com/ib-rest/1.0/installed-product/NIC:458513/allServiceContracts.xml
</url>
</allServiceContracts>
</InstalledProduct>
</InstalledProducts>
这是处理InstalledProduct RootElement
的Java类import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.log4j.Logger;
@XmlRootElement(name = "InstalledProduct")
public class InstalledProductDTO {
private ContractsURL latestServiceContracts;
private PartNumber partNumber;
public static Logger logger = Logger.getLogger(InstalledProductDTO.class);
private int instanceId;
private String status;
@XmlElement(name = "serviceStatus")
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@XmlElement(name = "instanceId")
public int getInstanceId() {
return instanceId;
}
public void setInstanceId(int instanceId) {
this.instanceId = instanceId;
}
@XmlElement(name = "partNumber")
public PartNumber getpartNumberURN(){
return partNumber;
}
public void setProductNumber(PartNumber partNumber){
this.partNumber = partNumber;
}
@XmlElement(name = "latestServiceContracts")
public ContractsURL getLatestServiceContractsURL(){
return latestServiceContracts;
}
public void setAllServiceContractsURL(ContractsURL latestServiceContractsURL){
this.latestServiceContracts = latestServiceContractsURL;
}
}
这是处理partNumber标记的类,这是问题所在。
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "partNumber")
public class PartNumber {
private String urn;
@XmlElement(name = "urn")
public String getURN()
{
return urn;
}
public void setUrn(String urn){
this.urn = urn;
}
}
当我尝试获取partNumber时,我得到一个null。有任何想法吗?
答案 0 :(得分:0)
好的,我刚解决了。至少这对我有用。 检查在InstalledProductDTO类中,partNumber xml标记的sets和gets与文本xml标记不匹配。标签是“partNumber”,但是get和set被称为getPartNumberURN和setPartNumberURN。所以我只是把它们改成了getPartNumber和setPartNumber,这很有效。 如果这对你有用,请告诉我确认一下。如果我弄错了,请纠正我。 谢谢,希望这有帮助!