0

每个人!我正在开发这个宁静的 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 时,我得到一个空值。有任何想法吗?

4

1 回答 1

0

好的,我刚刚解决了。至少这对我有用。检查在 InstalledProductDTO 类中,partNumber xml 标记的集合和获取与文字 xml 标记不匹配。标签是“partNumber”,但 get 和 set 被称为 getPartNumberURN 和 setPartNumberURN。所以我只是将它们更改为 getPartNumber 和 setPartNumber,这很有效。如果这对您有用,请让我知道以确认这一点。如果我错了,请纠正我。谢谢,希望这会有所帮助!

于 2012-05-02T14:44:51.197 回答