How to use Retrofit and SimpleXML together in downloading and parsing an XML file from a site?
Solution 1
You will create an interface as a new class in your project:
public interface ApiService {
@GET("/xml/simple.xml")
YourObject getUser();
}
Then in your activity you will call the following:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://www.w3schools.com")
.setConverter(new SimpleXmlConverter())
.build();
ApiService apiService = restAdapter.create(ApiService.class);
YourObject object = apiService.getXML();
To get your libraries correctly, in your build.gradle file you need to do the following:
configurations {
compile.exclude group: 'stax'
compile.exclude group: 'xpp3'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.mobprofs:retrofit-simplexmlconverter:1.1'
compile 'org.simpleframework:simple-xml:2.7.1'
compile 'com.google.code.gson:gson:2.2.4'
}
Then you need to specify YourObject and add annotations to it according to the structure of the xml file
@Root(name = "breakfast_menu")
public class BreakFastMenu {
@ElementList(inline = true)
List<Food> foodList;
}
@Root(name="food")
public class Food {
@Element(name = "name")
String name;
@Element(name = "price")
String price;
@Element(name = "description")
String description;
@Element(name = "calories")
String calories;
}
Solution 2
import java.util.ArrayList;
import java.util.List;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
@Root(name = "breakfast_menu")
public class BrakfastMenu
{
@ElementList(inline = true)
protected List<Food> food;
public List<Food> getConfigurations()
{
if (food == null)
{
food = new ArrayList<Food>();
}
return this.food;
}
public void setConfigurations(List<Food> configuration)
{
this.food = configuration;
}
}
Solution 3
Here's how to do it with Retrofit 2.
First you need an interface like (headers annotations are optional) :
public interface ApiService
{
@GET("xml/simple.xml")
@Headers({"Accept: application/xml",
"User-Agent: Retrofit-Sample-App"})
Call<BreakfastMenu> getBreakfastMenu();
}
The annotated POJOs for XML are the same as in the other answers.
Then you need to make a request to the server :
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.w3schools.com/")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
ApiService apiService = retrofit.create(ApiService.class);
Call<BreakfastMenu> call = apiService.getBreakfastMenu();
Response<BreakfastMenu> response = call.execute();
// response.code() == 200
BreakfastMenu breakfastMenu = response.body();
The needed libraries are :
- retrofit 2.3.0
- okhttp 3.8.0
- converter-simplexml 2.3.0
- simple-xml 2.7.1
- Java 7
Source available on my GitHub
Related videos on Youtube
greenspand
Android ⚡ Java developer and more and more a web 🌞 development and 🔥Firefox OS enthusiast.I mostly do Android development and am quite fresh 🏄 in programming .I try to grasp 🐙 new concepts and learn as much as I can and, in the process, share♻ and give back🚀 .I 💖 Love the web. It's the past 👻 present and future. It's formless holistic shape give it a special kind of leaderless power💪. I would dare to say it's the human species nervous system. Got side tracked right here 😊 My long term goal is to do more and more web cross device, platform independent 🔐work. Reasons are all too clear and the possibilities are all too many.
Updated on November 17, 2020Comments
-
greenspand over 3 years
I just started working with Retrofit. I am working on a project that uses SimpleXML. Can somebody provide me an example in which one fetches an XML from a site e.g. http://www.w3schools.com/xml/simple.xml" and reads it out?
-
greenspand almost 10 yearsIn this case is my Object the first Node of the Elements Tree, e.g.
-
vandus almost 10 yearsYes, I am going to provide you with an example of this structure
-
greenspand almost 10 yearsAham awesome. I did the structure based on the xml example. It starts with this Model:
-
vandus almost 10 yearsYep. I have updated my answer with the structure for you. You can also make it simpler, but it is up to you, what you want to do with it.
-
Red M almost 8 yearsI know that this is a late reply, but I'm trying to understand what does "inLine = true" do?
-
Farshad Tahmasbi about 7 yearsIn case you're looking for a more detailed example: futurestud.io/tutorials/retrofit-how-to-integrate-xml-converter
-
charitha amarasinghe almost 7 yearshi, can you help me to do the same with Retrofit 2 , instead of using "RestAdapter" (but using Retrofit - > . Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_BASE_URL) .client(new OkHttpClient()) .addConverterFactory(SimpleXmlConverterFactory.create()) .build();)
-
Dr.jacky almost 7 years@vandus What is getXML()?!
-
Pavlos about 6 yearswhat about Kotlin?