Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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_Sax - Fatal编程技术网

Android 如何解析本地xml文件以获取国家/地区详细信息?

Android 如何解析本地xml文件以获取国家/地区详细信息?,android,xml,sax,Android,Xml,Sax,我必须解析xml文件以获得国家详细信息,如国家名称和国家邮政编码。 如何将国家名称解析为微调器适配器,当我使用微调器选择特定国家时,我必须在textview中显示特定的国家代码 请帮帮我 提前感谢。这里有一个解析Xml文件的代码,您必须在其中传递本地Xml文件的inputstream。 public static ArrayList<Country> parseCountry(Context context, InputStream inputStream) { String

我必须解析xml文件以获得国家详细信息,如国家名称和国家邮政编码。 如何将国家名称解析为微调器适配器,当我使用微调器选择特定国家时,我必须在textview中显示特定的国家代码

请帮帮我


提前感谢。

这里有一个解析Xml文件的代码,您必须在其中传递本地Xml文件的inputstream。

public static ArrayList<Country> parseCountry(Context context, InputStream inputStream) {
    String KEY = "";
    String VALUE = null;
    ArrayList<Country> arrCountires = new ArrayList<Country>();
    Country country = null;
    ArrayList<State> arrStates = null;
    State state= null;
    ArrayList<City> arrCities = null;
    City city = null;

    try {
        InputStreamReader inputreader = null;
        if(inputStream != null) {
            inputreader = new InputStreamReader(inputStream);
        }

        if(inputreader != null) {
            XmlPullParserFactory factory = null;
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = null;
            xpp = factory.newPullParser();
            xpp.setInput(inputreader);
            int eventType = 0;
            eventType = xpp.getEventType();
            eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if(eventType == XmlPullParser.START_TAG) {
                    KEY = xpp.getName();
                    if(KEY.equalsIgnoreCase(TAGS.COUNTRIES)) {
                        arrCountires = new ArrayList<Country>();

                    }else if(KEY.equalsIgnoreCase(TAGS.COUNTRY)) {
                        country = new Country();
                        arrStates = new ArrayList<State>();
                        country.setCountryId(xpp.getAttributeValue(null, TAGS.ID));

                    }else if(KEY.equalsIgnoreCase(TAGS.STATE)) {
                        state = new State();
                        arrCities = new ArrayList<City>();
                        state.setStateId(xpp.getAttributeValue(null, TAGS.ID));

                    }else if(KEY.equalsIgnoreCase(TAGS.CITY)) {
                        city = new City();
                        city.setCityId(xpp.getAttributeValue(null, TAGS.ID));
                    }
                }else if(eventType == XmlPullParser.END_TAG) {
                    KEY = xpp.getName();
                    if(KEY.equalsIgnoreCase(TAGS.COUNTRY)) {
                        country.setArrStates(arrStates);
                        arrCountires.add(country);

                    }else if(KEY.equalsIgnoreCase(TAGS.COUNTRY_NAME)) {
                        country.setCountryName(VALUE);

                    }else if(KEY.equalsIgnoreCase(TAGS.STATE_NAME)) {
                        state.setStateName(VALUE);

                    }else if(KEY.equalsIgnoreCase(TAGS.STATE)) {
                        state.setArrCities(arrCities);
                        arrStates.add(state);

                    }else if(KEY.equalsIgnoreCase(TAGS.CITY)) {
                        arrCities.add(city);
                    }else if(KEY.equalsIgnoreCase(TAGS.CITY_NAME)) {
                        city.setCityName(VALUE);
                    }
                }else if(eventType == XmlPullParser.TEXT) {
                    VALUE = xpp.getText();
                }
                eventType = xpp.next();
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }finally {
        if(inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return arrCountires;
}
public class Country {

String countryId;
String countryName;
ArrayList<State> arrStates;
public ArrayList<State> getArrStates() {
    return arrStates;
}
public void setArrStates(ArrayList<State> arrStates) {
    this.arrStates = arrStates;
}
public String getCountryId() {
    return countryId;
}
public void setCountryId(String countryId) {
    this.countryId = countryId;
}
public String getCountryName() {
    return countryName;
}
public void setCountryName(String countryName) {
    this.countryName = countryName;
}

}
private class CountryAdapter implements SpinnerAdapter{
ArrayList<Country> data;

public CountryAdapter(ArrayList<Country> data){
    this.data = data;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return android.R.layout.simple_spinner_dropdown_item;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView v = new TextView(getApplicationContext());
    v.setTextColor(Color.BLACK);
    v.setText(data.get(position).getName());
    v.setTextSize(15);
    v.setPadding(10, 10, 10, 10);
    v.setSingleLine();
    v.setEllipsize(TruncateAt.END);

    return v;
}

@Override
public int getViewTypeCount() {
    return android.R.layout.simple_spinner_item;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isEmpty() {
    return false;
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {

}

@Override
public View getDropDownView(int position, View convertView,
    ViewGroup parent) {
    return this.getView(position, convertView, parent);
}
}
这里有一个代码,可以从资产中将文件作为inputstream打开

try {
    InputStream inputStream = v.getContext().getAssets().open("path of file");
    ArrayList<Country> arrCountries = parseCountry(this, inputStream);

} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
spCountry.setOnItemSelectedListener(OnCountrySelected);
try {
    InputStream inputStream = v.getContext().getAssets().open("path of file");
    ArrayList<Country> arrCountries = parseCountry(this, inputStream);

} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
CountryAdapter countryAdapter = new CountryAdapter(arrCountry);
            spCountry.setAdapter(countryAdapter);