7

I made a webservice in java with a method that returns a string (a generic list in XML format). I consume this webservice from Android, and I get this string, but after several tries the Android emulator just crashes when trying to deserialize the string. This is an example for the string I get:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<peliculas>
    <pelicula>
        <id>18329</id>
        <poster>http://cache-cmx.netmx.mx/image/muestras/5368.rrr.jpg</poster>
        <titulo>007 Operaci&amp;oacute;n Skyfall</titulo>
    </pelicula>
...
</peliculas>

This is the class in the webservice:

@XmlRootElement
public class Peliculas{

    @XmlElement(name="pelicula")
    protected List<Pelicula> peliculas;
    public Peliculas(){ peliculas = new ArrayList<Pelicula>();}

    public Peliculas(List<Pelicula> pe){
        peliculas = pe;
    }


    public List<Pelicula> getList(){
        return peliculas;       
    }

    public void add(Pelicula pelicula) {
        peliculas.add(pelicula);
    }
}

________EDIT______________

Seems like you can't use JAXB with Android, and there's better/lighter libraries for that. so I tried Simple XML. This is the method:

public Peliculas unmarshal(String xml) throws Exception{            
    Peliculas peliculas = new Peliculas();  
    Serializer serializer = new Persister();
    StringBuffer xmlStr = new StringBuffer( xml );
    peliculas = serializer.read(Peliculas.class, ( new StringReader( xmlStr.toString() ) )  );
    return peliculas;
}

BUT I get this exception, seems like it can't save data in object:

11-12 20:30:10.898: I/Error(1058): Element 'Pelicula' does not have a match in class app.cinemexservice.Pelicula at line 3
4

2 回答 2

1

我认为你做得对,试试 API 中给出的这段代码。

JAXBContext jc = JAXBContext.newInstance( "add your class's full qualified class name here" );
Unmarshaller u = jc.createUnmarshaller();
Object o = u.unmarshal( xmlSource );

我认为您可以将 Object o 转换为您的类型。请参考这里。http://jaxb.java.net/nonav/2.2.4/docs/api/javax/xml/bind/Unmarshaller.html

于 2012-11-12T01:44:07.587 回答
0

我使用 SAX 解析文件,然后手动将其转换为对象。这是代码:

public List<Pelicula> unmarshal(String xml) throws Exception{           
        List<Pelicula> peliculas = new ArrayList<Pelicula>();       
        InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
        XmlPullParser parser = Xml.newPullParser(); 
        char[] c;
        String id="", titulo="", poster="", atributo="";
        int datos =0;
        try{ 
            parser.setInput(is, "UTF-8"); 
            int event = parser.next();  
        while(event != XmlPullParser.END_DOCUMENT) { 
            if(event == XmlPullParser.START_TAG) { 
                Log.d(TAG, "<"+ parser.getName() + ">"); 
                atributo = parser.getName();
                for(int i = 0; i < parser.getAttributeCount(); i++) { 
                    Log.d(TAG, "\t"+ parser.getAttributeName(i) + " = "+ parser.getAttributeValue(i)); 
                } 
            } 
            if(event == XmlPullParser.TEXT&& parser.getText().trim().length() != 0) 
            {
                Log.d(TAG, "\t\t"+ parser.getText());
                if (atributo=="id"){id=parser.getText(); datos++;}
                else if(atributo=="titulo"){titulo=parser.getText(); datos++;}
                else if(atributo=="poster"){poster=parser.getText(); datos++;}
                if(datos==3){peliculas.add(new Pelicula(id, titulo, poster)); datos=0;} 
            }
                if(event == XmlPullParser.END_TAG) 
                    Log.d(TAG, "</"+ parser.getName() + ">");               
                event = parser.next(); 

            is.close();
        }
        } catch(Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); }        
        for (Pelicula p : peliculas){
            Log.d("Película en lista: ", p.titulo);
        }           
        return peliculas;
    }

这对我的口味来说太长了,但我就是想不出简单的 XML 来匹配我的类。

于 2012-11-13T21:22:56.450 回答