如何从xml文件中获取URL作为字符串?

时间:2018-05-08 15:46:59

标签: java android xml url xml-parsing

这听起来像一个简单的问题,但我还没有找到答案。

我有一个.xml文件包含不同的项目,包括一个URL项目。我知道那个URL字符串项的开头。 在其他项目中找到此URL并将其作为字符串获取的最简单方法是什么?

这是一个检查xml文件是否存在的类。 如果存在,我应该从中获取一个网址。

public class ConfigFile {

    private Context context;
    private static String url;

    public ConfigFile (Context _context, String _url){
        this.context = _context;
        this.url = _url;
    }

    public static String getUrl() {
        return url;
    }

    public static void setUrl(String url) {
        ConfigFile.url = url;
    }


    public void checkExistence(){
        File file = new File(context.getApplicationContext().getFilesDir(),"configfile.xml");
        if(file.exists()){
            // here I should paste code which returns url to set it as connection
            //setUrl();
        }
        else{
            //smth
        }

    }

1 个答案:

答案 0 :(得分:1)

只需使用Android内置的基本xml解析框架。

关于XML解析的基本Android文档。 https://developer.android.com/training/basics/network-ops/xml

如果你发现那个太复杂,那么使用一个基于注释的。 SimpleXML是我过去使用过的并且效果很好的。

http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php

相关问题