Possible Duplicate:
Best XML Parser for PHP
Using JAXB with Google Android
I need to add a simple functionality into my Android app.
I must connect to this fictional script: http://test/getMagazinesList.php
The script will return me a XML file like this:
<magazines>
<magazine title="APP 1" id="1">
<description>Prueba real</description>
<miniature>http://web/miniature.jpg</miniature>
</magazine>
<magazine title="APP 2" id="2">
<description>Prueba real 2</description>
<miniature>http://web/miniature2.jpg</miniature>
</magazine>
<magazine title="APP 3" id="3">
<description>Prueba real 3</description>
<miniature>http://web/miniature3.jpg</miniature>
</magazine>
</magazines>
The objective is to store all the info of the XML in a List of these MagazinePreview objects:
public class MagazinePreview {
String title;
String id;
String description;
String miniatureUrl;
Bitmap miniature;
public MagazinePreview(String title, String id, String description,
String miniatureUrl) {
super();
this.title = title;
this.id = id;
this.description = description;
this.miniatureUrl = miniatureUrl;
}
}
Which is the easiest approach to connect to that PHP and get the XML and interpret it storing the info on the List of MagazinePreview class objects?
Thanks
You can use Simple XML for this.
Adorn your
MagazinePreviewclass with these annotations:Now add a class for your collection of magazines. Add a method that performs deserialization of an XML string, instantiating your objects as it does so:
This will get you a
MagazinePreviewsinstance with yourMagazinePreviewinstances in it. Simple XML does all the heavy lifting. It will use the annotations (@Element,@Attribute) to know which data to place in which fields.You’ll still need a way to connect to your PHP server script. Assuming it’s a script that simply returns a string of XML, you could use this:
This code calls your PHP script on the server, retrieves the response (assumed to be an XML string), or throws an error if the request fails.