从XML反序列化/解组通用列表以在Android中列出

时间:2012-11-11 20:08:54

标签: java android web-services jaxb marshalling

我在java中创建了一个web服务,其方法是返回一个字符串(XML格式的通用列表)。我从Android使用这个web服务,我得到了这个字符串,但经过几次尝试后,Android模拟器在尝试反序列化字符串时崩溃了。这是我得到的字符串的示例:

<?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>

这是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);
    }
}

__ _ __ _ __ 修改 _ __ _ __ _ __ _ __ _ _ < /强>

好像你不能在Android上使用JAXB,并且有更好/更轻的库。所以我尝试了Simple XML。这是方法:

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;
}

但是我得到了这个异常,好像它无法在对象中保存数据:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;
    }

这对我来说太长了,但是我无法弄清楚Simple XML与我的课程相匹配。