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&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
I think you are doing correct, Try this code which is given in the API.
JAXBContext jc = JAXBContext.newInstance( "add your class's full qualified class name here" );
Unmarshaller u = jc.createUnmarshaller();
Object o = u.unmarshal( xmlSource );
You can cast the Object o to your type I think. Please refer this. http://jaxb.java.net/nonav/2.2.4/docs/api/javax/xml/bind/Unmarshaller.html
I used SAX to parse the file, and then convert 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's way too long for my taste, but I just couldn't figure out Simple XML to match my classes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.