如何使用XmlPullParser解析xml

时间:2012-05-31 06:16:30

标签: android xml hashmap xmlpullparser linkedhashmap

我正在尝试使用XmlPullParser以这样的格式解析xml文件

<BOOK bnumber="1">
<CHAPTER cnumber="1">
<VERS vnumber="1">abc</VERS>
<VERS vnumber="2">cde</VERS>
<VERS vnumber="3">fgh</VERS>
</CHAPTER>
<CHAPTER cnumber="2">
<VERS vnumber="1">abc</VERS>
<VERS vnumber="2">cde</VERS>
<VERS vnumber="3">fgh</VERS>
</CHAPTER>
</BOOK>

以下是我尝试开发的代码。我想要做的是将数据放入一个数组,以便我可以调用数据并在以后使用它。您能就如何修改代码以达到我的目的提出一些建议吗?

ArrayList<LinkedHashMap<String, ArrayList<LinkedHashMap<String, String>>>> array;
ArrayList<LinkedHashMap<String, String>> array2;
LinkedHashMap map,map_1 ;   


try {
                /** Handling XML */
                    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(true);
                XmlPullParser xpp = factory.newPullParser();
            /** Handling XML */


            /** Send URL to parse XML Tags */
            URL sourceUrl = new URL(
                    "http://123.com/mobile/1.xml");
            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
             xpp.setInput(sourceUrl.openStream(), null);
             int eventType = xpp.getEventType();
             String currentTag = null;

             while (eventType != XmlPullParser.END_DOCUMENT) {
                 switch (eventType) {

                 case XmlPullParser.START_DOCUMENT:
                     System.out.println("START_DOCUMENT "+xpp.getName());

                     array = new ArrayList<LinkedHashMap<String, ArrayList<LinkedHashMap<String, String>>>>();

                     break;


                 case XmlPullParser.START_TAG:

                     if ("CHAPTER".equals(xpp.getName()))
                     {
                     array2 = new ArrayList<LinkedHashMap<String, String>>();
                     map_1 = new LinkedHashMap<String, Object>();
                     map_1.put("cnumber",xpp.getAttributeValue(0));

                     }
                     else if ("VERS".equals(xpp.getName()))
                     {
                     map = new LinkedHashMap<String, Object>();
                     String Vers_number = xpp.getAttributeValue(0);
                     String Vers_content=xpp.nextText();
                     map.put("vnumber",Vers_number);
                     map.put("vcontent",Vers_content);
                     array2.add(map);
                     }

                     break;


                 case XmlPullParser.END_TAG:
                     if ("CHAPTER".equals(xpp.getName()))
                     {
                    map_1.put("in",array2);
                    array.add(map_1);}
                     break;  
                 }

                 eventType = xpp.next();  
             }

             itemCount=array.size();

            }catch (Exception e) {
                System.out.println("XML Pasing Excpetion = " + e);
            }

获取数据的代码[我无法弄清楚如何做到这一点,以下是我脑海中的想法]

try{  
            for(int i=0;i<itemCount;i++)
            {

        ArrayList<LinkedHashMap<String, String>> ch=array.get(i).get("cnumber");
        ArrayList<LinkedHashMap<String, String>> in=array.get(i).get("in");



            }

0 个答案:

没有答案