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

时间:2012-01-03 10:02:20

标签: android xml sax

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

请帮帮我。

提前致谢。

1 个答案:

答案 0 :(得分:1)

以下是解析Xml文件的代码,您必须在该文件中传递本地xml文件的输入流。

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;
}

这是一个包含Getter和Setter方法的Country类。

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);
}
}

以下是一个界面,您可以通过该界面从微调器中获取所选国家/地区

OnItemSelectedListener OnCountrySelected = new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View v, int position,
        long id) {
    if(position != AdapterView.INVALID_POSITION) {
        System.out.println("Country name = " + arrCountries.get(position).getName());   
        //Here you can set this value to the textview
    }
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
};

以下是将侦听器设置为微调器的方法

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();
}

当你在Country数组中得到响应然后将适配器设置为微调器

CountryAdapter countryAdapter = new CountryAdapter(arrCountry);
            spCountry.setAdapter(countryAdapter);