【问题标题】:How to get URL as a string from xml file?如何从 xml 文件中获取 URL 作为字符串?
【发布时间】:2018-05-08 15:46:59
【问题描述】:

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

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

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

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
        }

    }

【问题讨论】:

  • 在您投反对票之前,请提供 XML 文件以及到目前为止您编写的代码
  • @Deepakkaku 我添加了我编写的代码。问题是我不确定 xml 文件的其他项目,但我知道会有一个我需要的 url

标签: java android xml url xml-parsing


【解决方案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

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 2011-05-30
    • 2016-03-14
    • 2013-11-07
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2019-04-14
    相关资源
    最近更新 更多