0

如何编写 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

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

谢谢..

4

1 回答 1

0

在这里看到了一个读取复杂 XML 数据的示例……它对我解决问题有很大帮助。希望这可以帮助像我这样的人..

于 2013-09-04T11:29:37.660 回答