How to parse the xml by using XmlPullParser

I am trying to use XmlPullParser to parse the xml file in a format like that\[code\]<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>\[/code\]The following is the code I try to develop. What I want to do is put the data into an array so that i can call the data back and use it later. Can u give some advice on how to modify the code to suit for my purpose?\[code\]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); }\[/code\]Code to get back the data[I can't figure out how to do it, the following is and idea in my mind]\[code\]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"); }\[/code\]
 
Back
Top