使用XStream读取XML

时间:2013-09-04 09:31:26

标签: java xstream

如何编写java类来读取此XMl文件

<Response> 
<Models Make="SAMSUNG"> 
        <ModelNo>AB0-12343</ModelNo> 
        <ModelNo>BB1232222</ModelNo> 
    </Models>
     <Models Make="PANASONIC"> 
        <ModelNo>AB0-12343</ModelNo>
        <ModelNo>BB1232222</ModelNo> 
    </Models> 
</Response>

我试过这个

@XStreamAlias("Response")
public class SL_LookupModelsResponse  {
    private ArrayList<Models> Models;

    public ArrayList<Models> getModels() {
        return Models;
    }

    public void setModels(ArrayList<Models> models) {
        Models = models;
    }

    @Override
    public String toString() {
        return "SL_LookupModelsResponse [Models=" + Models + "]";
    }

    @XStreamAlias("Models")
    class Models{
        private ArrayList<String> ModelNo;

        @XStreamAsAttribute  
        private String Make;  

        public ArrayList<String> getModelNos() {
            return ModelNo;
        }

        public void setModelNos(ArrayList<String> ModelNo) {
            this.ModelNo = ModelNo;
        }

        public String getMake() {  
            return Make;  
        }  

        public void setMake(String make) {  
            this.Make = make;  
        }  

        @Override
        public String toString() {
            return "Models [ModelNo=" + ModelNo + "Make="+ Make +"]";
        }

    }
  }

但我得到了这个错误

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: ModelNo : ModelNo
---- Debugging information ----
message             : ModelNo
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : ModelNo
class               : java.util.ArrayList
required-type       : java.util.ArrayList
converter-type      : com.thoughtworks.xstream.converters.collections.CollectionConverter
path                : /Response/Models/ModelNo
class[1]            : com.samples.SL_LookupModelsResponse
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : null

请帮我解决这个问题......

谢谢..

1 个答案:

答案 0 :(得分:0)

我看到了一个用于阅读复杂XML数据的示例here ...它帮助我解决了这个问题。希望这有助于像我这样的人..