Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

android use xmlpullparser parse xml from url

I use Xmlpullparser to parse xml from Asset folder
this is code

public class FragmentImg extends Fragment {
static final String KEY_EMP = "emp";
static final String KEY_NAME = "name";
static final String KEY_GENDER = "gender";
static final String KEY_AGE = "age";
List<HashMap<String,String>> imgHashmap;
List<ClassImg> imgList = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_img, container, false);
    ImgActivity activity = (ImgActivity) getActivity();
    String uri = "list_img.xml";
    GridView gv_img = (GridView)view.findViewById(R.id.gridViewImg);
    try {
        XmlPullParserImg parser_Img = new XmlPullParserImg();
        imgList = parser_Img.parse(getActivity().getAssets().open(uri));
        BinderDataImg bd_img = new BinderDataImg(getActivity(), imgHashmap);
        gv_img.setAdapter(bd_img);
        gv_img.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent i = new Intent();
                i.setClass(getActivity(), ImgDetail.class);
                startActivity(i);
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return view;
}
public class XmlPullParserImg {
    private ClassImg c_i;
    private String text;
    public XmlPullParserImg() {
        danhsachList = new ArrayList<ClassImg>();        }
    public List<ClassImg> parse(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            imgHashmap = new ArrayList<HashMap<String,String>>();
            HashMap<String,String> map = null;
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
            parser.setInput(is, null);
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        if (tagname.equalsIgnoreCase("KEY_EMP")) {
                            c_i = new ClassImg();
                            map = new HashMap<String,String>();
                        }
                        break;
                    case XmlPullParser.TEXT:
                        text = parser.getText();
                        break;
                    case XmlPullParser.END_TAG:
                        if (tagname.equalsIgnoreCase("KEY_EMP")) {
                            imgList.add(c_ds);
                            imgHashmap.add(map);
                        } else if (tagname.equalsIgnoreCase(KEY_NAME)) {
                            c_i.setName(text);
                            map.put(KEY_NAME, text);
                        } else if (tagname.equalsIgnoreCase(KEY_GENDER)) {
                            c_i.setGender(text);
                            map.put(KEY_GENDER, text);
                        } else if (tagname.equalsIgnoreCase(KEY_AGE)) {
                            c_i.setAge(text);
                            map.put(KEY_AGE, text);
                        break;
                    default:
                        break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imgList;
    }
}

}

Everything is ok, and after that, i upload xml to dropbox.
this is url https://www.dropbox.com/s/lh2ucpbvpqloa3e/list_img.xml
how to parse it with my code?
thank you for reading.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try like this

URL url=new URL("https://www.dropbox.com/s/lh2ucpbvpqloa3e/list_img.xml");
HttpURLConnection http=(HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.connect();
InputStream is=http.getInputStream();

After that you can assign inputstream to setData() function

  parser.setInput(is, null);

And rest of the code you already written


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...