2014-03-12 108 views
-1

我正在创建一个Web服务的客户端 - “www.webservicex.net/uszip.asmx”,它返回所有城市,并从那个状态返回邮政编码(我会将这些信息用于天气广播节目)。Web服务返回一个对象,我无法使用JAVA

ipSoap.getInfoByState()返回一个对象GetInfoByStateResult(夹条氟利昂现在)。现在问题是我无法访问内容gibs了。 getContent()方法返回一个对象列表,但它没有方法让我得到我想要的数据(名称和拉链)。

如何从响应中获取城市和拉链的名称?

这里是我的代码:

public class Main { 

    public static void main(String[] args){ 
     USZip ip = new USZip(); 
     USZipSoap ipSoap = ip.getUSZipSoap(); 
     GetInfoByStateResult gibs = ipSoap.getInfoByState("NJ"); //New York 
     List<Object> listOfCities = gibs.getContent(); 
       /* problem here, getContent is always size 1, and i can't see what's inside in it. gibs.getIndex(0).toString returns: [NewDataSet: null]. 
*/ 

     } 
} 

GetCityByResult是在底部

/** 
* <p>Java class for anonymous complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="GetInfoByCityResult" minOccurs="0"> 
*   &lt;complexType> 
*    &lt;complexContent> 
*    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*     &lt;sequence> 
*     &lt;any/> 
*     &lt;/sequence> 
*    &lt;/restriction> 
*    &lt;/complexContent> 
*   &lt;/complexType> 
*   &lt;/element> 
*  &lt;/sequence> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "getInfoByCityResult" 
}) 
@XmlRootElement(name = "GetInfoByCityResponse") 
public class GetInfoByCityResponse { 

    @XmlElement(name = "GetInfoByCityResult") 
    protected GetInfoByCityResponse.GetInfoByCityResult getInfoByCityResult; 

    /** 
    * Gets the value of the getInfoByCityResult property. 
    * 
    * @return 
    *  possible object is 
    *  {@link GetInfoByCityResponse.GetInfoByCityResult } 
    *  
    */ 
    public GetInfoByCityResponse.GetInfoByCityResult getGetInfoByCityResult() { 
     return getInfoByCityResult; 
    } 

    /** 
    * Sets the value of the getInfoByCityResult property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link GetInfoByCityResponse.GetInfoByCityResult } 
    *  
    */ 
    public void setGetInfoByCityResult(GetInfoByCityResponse.GetInfoByCityResult value) { 
     this.getInfoByCityResult = value; 
    } 


    /** 
    * <p>Java class for anonymous complex type. 
    * 
    * <p>The following schema fragment specifies the expected content contained within this class. 
    * 
    * <pre> 
    * &lt;complexType> 
    * &lt;complexContent> 
    *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *  &lt;sequence> 
    *   &lt;any/> 
    *  &lt;/sequence> 
    *  &lt;/restriction> 
    * &lt;/complexContent> 
    * &lt;/complexType> 
    * </pre> 
    * 
    * 
    */ 
    @XmlAccessorType(XmlAccessType.FIELD) 
    @XmlType(name = "", propOrder = { 
     "content" 
    }) 
    public static class GetInfoByCityResult { 

     @XmlMixed 
     @XmlAnyElement(lax = true) 
     protected List<Object> content; 

     /** 
     * Gets the value of the content property. 
     * 
     * <p> 
     * This accessor method returns a reference to the live list, 
     * not a snapshot. Therefore any modification you make to the 
     * returned list will be present inside the JAXB object. 
     * This is why there is not a <CODE>set</CODE> method for the content property. 
     * 
     * <p> 
     * For example, to add a new item, do as follows: 
     * <pre> 
     * getContent().add(newItem); 
     * </pre> 
     * 
     * 
     * <p> 
     * Objects of the following type(s) are allowed in the list 
     * {@link String } 
     * {@link Object } 
     * 
     * 
     */ 
     public List<Object> getContent() { 
      if (content == null) { 
       content = new ArrayList<Object>(); 
      } 
      return this.content; 
     } 
    } 
} 

回答

2

web服务似乎回到非常通用的XML,通过该模式中的any元素所示。

按照JAXB documentation

JAXB结合任何这样的元素到一个对象,并在解组, 所有遇到元件被解组成对应JAXB 对象(包括JAXBElements如果必要的话),并放置在该 领域。如果遇到无法解组的元素,则生成DOM 元素。

这意味着如果GetInfoByCityResult包含符合您为其生成的Java类的模式的元素,则将返回这些类的实例;否则将返回原始DOM元素。

恕我直言,服务提供商使用这样的一般模式有点松懈 - 为了消费XML,除了通用显示元素之外,您需要能够为这些元素附加含义,如果它们没有在模式中定义是不可能的。

你的情况,这意味着你将需要调试代码,并期待在由Web服务返回的元素,那么代码以这些元素(希望他们不改变)。

相关问题