Deserialize / unmarshal general list from XML to list on Android

I created a web service in Java using a method that returns a string (general list in XML format). I use this web service from Android and I get this line, but after several attempts, the Android emulator just crashes when trying to deserialize the line. This is an example of the line 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 web service:

 @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 ______________

It looks like you cannot use JAXB with Android, and this requires more / less libraries. 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, it looks like it cannot save data in the 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 
+7
source share
2 answers

I used SAX to parse the file and then converted it manually to an object. This is the code:

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

It is too long for my taste, but I just could not understand Simple XML to fit my classes.

0
source

I think you are doing it right, try this code, which is specified in the API.

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

You can use Object o for your type, I think. Please refer to this. http://jaxb.java.net/nonav/2.2.4/docs/api/javax/xml/bind/Unmarshaller.html

+1
source

All Articles