代码之家  ›  专栏  ›  技术社区  ›  Jeavie

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

  •  0
  • Jeavie  · 技术社区  · 6 年前

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

    我有一个。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
            }
    
        }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Sam    6 年前

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

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

    如果您发现这个太复杂了,那么就使用基于注释的方法。 SimpleXML是我过去使用过的一种,它工作得很好。

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