簡體   English   中英

如何從XML文件獲取URL作為字符串?

[英]How to get URL as a string from xml file?

聽起來像一個簡單的問題,但我還沒有找到答案。

我有一個.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
        }

    }

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

有關XML解析的基本Android文檔。 https://developer.android.com/training/basics/network-ops/xml

如果您發現其中一項過於復雜,請使用基於注釋的一項。 SimpleXML是我過去使用過的並且效果很好。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM