Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android xml字符串资源中获取标记_Android_Xml_Android Layout_Android Xml_Android Xml Attribute - Fatal编程技术网

如何在android xml字符串资源中获取标记

如何在android xml字符串资源中获取标记,android,xml,android-layout,android-xml,android-xml-attribute,Android,Xml,Android Layout,Android Xml,Android Xml Attribute,我想在xml数组中获取标记,如country,countryCode,iso2,iso3 <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="country_data"> <item> <country>Afghanistan</country> <count

我想在xml数组中获取标记,如
country
countryCode
iso2
iso3

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="country_data">
        <item>
            <country>Afghanistan</country>
            <countryCode>93</countryCode>
            <iso2>AF</iso2>
            <iso3>AFG</iso3>
        </item>
        <item>
            <country>Albania</country>
            <countryCode>355</countryCode>
            <iso2>AL</iso2>
            <iso3>ALB</iso3>
        </item>
        <item>
            <country>Algeria</country>
            <countryCode>213</countryCode>
            <iso2>DZ</iso2>
            <iso3>DZA</iso3>
        </item>
        <item>
            <country>American Samoa</country>
            <countryCode>1-684</countryCode>
            <iso2>AS</iso2>
            <iso3>ASM</iso3>
        </item>
        <item>
            <country>Andorra</country>
            <countryCode>376</countryCode>
            <iso2>AD</iso2>
            <iso3>AND</iso3>
        </item>
        <item>
            <country>Angola</country>
            <countryCode>244</countryCode>
            <iso2>AO</iso2>
            <iso3>AGO</iso3>
        </item>
    </string-array>
</resources>

阿富汗
93
空军
AFG
阿尔巴尼亚
355
艾尔
ALB
阿尔及利亚
213
DZ
达扎
美属萨摩亚
1-684
作为
ASM
安道尔
376
公元
及
安哥拉
244
敖
以前

我想将
国家
国家代码
iso2
iso3
,全部独立地放入不同的ArrayList(ArrayList国家、国家代码、iso2、iso3)。

要访问
字符串数组
,可以执行以下操作:

String[] ar = getResources().getStringhArray(R.array.country_data);
但是,该数组是一维数组。那么,像这样的祝酒辞

Toast.makeText(this, ar[0], Toast.Length_SHORT).show();
将显示

Afghanistan 93 AF AFG
如果确定国家/地区名称中不会插入空格,则可以拆分字符串


如果
xml
是一个外部文件,那么最好使用
XmlPullParserFactory
来解析该文件。在这种情况下,您可以直接访问country、countryCode、iso2、iso3。

要访问字符串数组,可以执行以下操作:

String[] ar = getResources().getStringhArray(R.array.country_data);
但是,该数组是一维数组。那么,像这样的祝酒辞

Toast.makeText(this, ar[0], Toast.Length_SHORT).show();
将显示

Afghanistan 93 AF AFG
如果确定国家/地区名称中不会插入空格,则可以拆分字符串

如果
xml
是一个外部文件,那么最好使用
XmlPullParserFactory
来解析该文件。在这种情况下,您可以直接访问country、countryCode、iso2、iso3

我就是这样做的

我使用XmlPullParserFactory从assets文件夹中的xml文件中获取标记。

XmlPullParser的事件

XMLPullParser的next()方法将光标指针移动到下一个事件。通常,我们使用XMLPullParser接口中定义的四个常量(用作事件)。这些是:

开始标记
:已读取XML开始标记

TEXT
:读取了文本内容;可以使用getText()方法检索文本内容

结束标记
:已读取结束标记

结束文档
:没有更多可用事件

Android XMLPullParser XML解析

XMLPullParser
将检查包含一系列事件的XML文件,例如上面列出的用于解析XML文档的事件

要在android中使用XMLPullParser读取和解析XML数据,我们需要在android应用程序中创建XMLPullParserFactory、XMLPullParser对象的实例

下面是我在android应用程序中使用XMLPullParser读取和解析XML数据的代码,该应用程序使用XMLPullParserFactory、XMLPullParser和一系列事件从XML对象获取所需信息

然后将数据传递到BaseAdapter

package com.f.countryarraytest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    public TextView tvCountryName, tvCountryCode, tvCountryIso2;
    private CustomAdapter customAdapter;
    private static final String tagCountryItem = "country";
    private static final String tagCountryName = "countryName";
    private static final String tagCountryCode = "countryCode";
    private static final String tagCountryIso2 = "countryIso2";
    private static final String tagCountryIso3 = "countryIso3";
    private ArrayList<String> countryName, countryCode, countryIso2, countryIso3;
    private ArrayList<HashMap<String, String>> countryListArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView        = findViewById(R.id.listView);

        tvCountryName   = findViewById(R.id.tvMainActivity_CountryName);
        tvCountryCode   = findViewById(R.id.tvMainActivity_CountryCode);
        tvCountryIso2   = findViewById(R.id.tvMainActivity_CountryIso2);

        countryName     = new ArrayList<>();
        countryCode     = new ArrayList<>();
        countryIso2     = new ArrayList<>();
        countryIso3     = new ArrayList<>();

        try{
            countryListArray = new ArrayList<>();
            HashMap<String,String> country = new HashMap<>();

            InputStream inputStream             = getAssets().open("countries.xml");
            XmlPullParserFactory parserFactory  = XmlPullParserFactory.newInstance();
            parserFactory.setNamespaceAware(true);
            XmlPullParser parser                = parserFactory.newPullParser();

            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);
            parser.setInput(inputStream,null);

            String tag = "" , text = "";
            int event = parser.getEventType();

            while (event!= XmlPullParser.END_DOCUMENT){
                tag = parser.getName();
                switch (event){
                    case XmlPullParser.START_TAG:
                        if(tag.equals(tagCountryItem))
                            country = new HashMap<>();
                        break;
                    case XmlPullParser.TEXT:
                        text=parser.getText();
                        break;
                    case XmlPullParser.END_TAG:

                        if (tag.equalsIgnoreCase(tagCountryName)){
                            country.put(tagCountryName,text);

                        } else if (tag.equalsIgnoreCase(tagCountryCode)){
                            country.put(tagCountryCode,text);

                        } else if (tag.equalsIgnoreCase(tagCountryIso2)){
                            country.put(tagCountryIso2,text);

                        } else if (tag.equalsIgnoreCase(tagCountryIso3)){
                            country.put(tagCountryIso3,text);

                        } else if (tag.equalsIgnoreCase(tagCountryItem)){

                            if(country != null){
                                countryListArray.add(country);
                            }
                        }

                        /*switch (tag){
                            case tagCountryName: country.put(tagCountryName,text);
                                break;

                            case tagCountryCode: country.put(tagCountryCode,text);
                                break;

                            case tagCountryIso2: country.put(tagCountryIso2,text);
                                break;

                            case tagCountryIso3: country.put(tagCountryIso3,text);
                                break;

                            case tagCountryItem:
                                if(country != null){
                                    countryListArray.add(country);}
                                break;
                        }*/

                        break;
                }
                event = parser.next();
            }

            //Get ArrayList With County Data HashMap
            getHashMapData();

            customAdapter   = new CustomAdapter(this, countryName, countryCode, countryIso2, countryIso3);
            listView.setAdapter(customAdapter);
        }
        catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }


    }

    private void getHashMapData(){

        countryName.clear();
        countryCode.clear();
        countryIso2.clear();
        countryIso3.clear();

        try {

            if (countryListArray.size() > 0) {

                for (int i = 0; i < countryListArray.size(); i++) {

                    HashMap<String, String> hashmap = countryListArray.get(i);

                    countryName.add(hashmap.get(tagCountryName));
                    countryCode.add(hashmap.get(tagCountryCode));
                    countryIso2.add(hashmap.get(tagCountryIso2));
                    countryIso3.add(hashmap.get(tagCountryIso3));
                }
            }
        } catch (Exception e){}
    }

}
package com.f.countryarraytest;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.xmlpull.v1.XmlPullParser;
导入org.xmlpull.v1.XmlPullParserException;
导入org.xmlpull.v1.XmlPullParserFactory;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.HashMap;
公共类MainActivity扩展了AppCompatActivity{
私有列表视图列表视图;
公共文本视图tvCountryName、tvCountryCode、tvCountryIso2;
专用自定义适配器;
私有静态最终字符串tagCountryItem=“country”;
私有静态最终字符串tagCountryName=“countryName”;
私有静态最终字符串tagCountryCode=“countryCode”;
私有静态最终字符串tagCountryIso2=“countryIso2”;
私有静态最终字符串tagCountryIso3=“countryIso3”;
private ArrayList countryName、countryCode、countryIso2、countryIso3;
私有ArrayList countryListArray;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.listView);
tvCountryName=findViewById(R.id.TvMain活动\u CountryName);
tvCountryCode=findViewById(R.id.TVmain活动\u CountryCode);
tvCountryIso2=findViewById(R.id.TVmain活动\u CountryIso2);
countryName=新的ArrayList();
countryCode=新的ArrayList();
countryIso2=新的ArrayList();
countryIso3=新的ArrayList();
试一试{
countryListArray=newArrayList();
HashMap country=新的HashMap();
InputStream InputStream=getAssets().open(“countries.xml”);
XmlPullParserFactory parserFactory=XmlPullParserFactory.newInstance();
setNamespaceAware(true);
XmlPullParser=parserFactory.newPullParser();
setFeature(XmlPullParser.FEATURE_进程_名称空间,false);
setInput(inputStream,null);
字符串标记=”,文本=”;
int event=parser.getEventType();
while(event!=XmlPullParser.END_文档){
tag=parser.getName();
开关(事件){
case XmlPullParser.START_标记:
if(标记等于(标记国项目))
country=新的HashMap();
打破
case XmlPullParser.TEXT:
text=parser.getText();