Question

How to write java class to read this XMl file

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

I tried like this

@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 +"]";
        }

    }
  }

But Im getting this error

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

Please help me on how to solve this...

Thank you..

Was it helpful?

Solution

I see an example here for reading Complex XML data... It helped me a lot to solve the issue. Hope this helps somebody like me..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top