如何利用改造和SimpleXML一起下载,并从现场解析XML文件? [英] How to use Retrofit and SimpleXML together in downloading and parsing an XML file from a site?

查看:294
本文介绍了如何利用改造和SimpleXML一起下载,并从现场解析XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始与改造工作。我正在使用的SimpleXML项目。有人可以给我,其中一个从网站如取一个XML的例子 http://www.w3schools.com/xml/simple.xml 中记载其出?

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?

推荐答案

您将在项目中创建一个界面,一个新的类:

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();

要正确地得到您的图书馆,在build.gradle文件,你需要做到以下几点:

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

然后,你需要指定YourObject并根据XML文件的结构添加注释它

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

这篇关于如何利用改造和SimpleXML一起下载,并从现场解析XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆