简体   繁体   English

Android-从Web上的XML文件获取数据

[英]Android - Getting data from XML file on the web

I need to get data from an XML file in Android. 我需要从Android中的XML文件中获取数据。 On the iPhone environment, my code is: 在iPhone环境中,我的代码是:

NSURL *thisURL = [[NSURL alloc] initWithString:@"http://www.xxx.com/file.xml"];
NSArray *myArray = [[NSArray alloc] initWithContentsOfURL:providerURL];

myArray is now an array of dictionary items initialized with contents from file.xml. myArray现在是由file.xml中的内容初始化的字典项数组。

Is there any way to do this in Android? 有什么办法可以在Android中做到这一点吗? Can someone point me to doc or sample code? 有人可以指出我的文档或示例代码吗?

I'm new to the Android environment and just need some direction. 我是Android环境的新手,只需要一些指导。

Thanks, 谢谢,

Kevin 凯文

See Working with XML in Android for a variety of methods for dealing with XML. 有关处理XML的多种方法,请参见在Android中使用XML。 Which method to use depends on how big your XML is, and what you want to do with it. 使用哪种方法取决于XML的大小以及您要使用的XML。 ' '

I'm not sure how it makes any sense to turn XML into an array, so no, none of the methods do that. 我不确定将XML转换为数组有什么意义,所以不,没有一种方法可以做到这一点。 If you want something similar to that, use Json instead of XML. 如果您想要类似的东西,请使用Json而不是XML。

After a bit of research, it appears to me that using the Simple XML Serialization framework is going to be my best bet, especially since I do have a relatively simple XML file to read. 经过一番研究,在我看来,使用简单XML序列化框架将是我最好的选择,尤其是因为我确实有一个相对简单的XML文件可读取。 The result will be a 'list' class with several 'entry' classes which seems like a viable way to handle this...probably better than having an array of classes as was done in the iPhone app. 结果将是一个带有多个“ entry”类的“ list”类,这似乎是一种可行的处理方式……可能比在iPhone应用程序中拥有多个类要好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM