Pull Parse XML file
- Write an XML file yourself, save some weather information
Get the XML file
InputStream is = getClassLoader().getResourceAsStream("weather.xml");
Get the pull parser.
XmlPullParser xp = Xml.newPullParser();
Start parsing
Get the event type
int type = Xp.geteventtype ();
There are five main types of events
- START_ The event type of the Document:xml header
- end_document:xml tail
- start_tag: The event type of the start node
- end_tag: Event type of end node
- text: The event type of the text node
If the event type obtained is not end_document, the parsing is not complete, if it is, the parsing is complete, and the while loop ends
while (Type! = xmlpullparser.end_document)
When we resolve to different nodes, we need to do different things, so we can judge the name of the current node.
- When parsing to the start node of weather, the new list
- When resolving to the start node of city, the city object is created and the object is created to make it easier to save the text that will be parsed
When parsing to the name start node, gets the text content of the next node, same as temp, PM
Case xmlpullparser.start_tag://Gets the name of the current node if ("Weather". Equals (Xp.getname ())) {citys = new arraylist<city> () ; } else if ("City". Equals (Xp.getname ())) {city = new city (); } else if ("name". Equals (Xp.getname ())) {//Gets the text of the next node of the current node String name = Xp.nexttext (); City.setname (name); } else if ("temp". Equals (Xp.getname ())) {String temp = Xp.nexttext (); City.settemp (temp); } else if ("PM". Equals (Xp.getname ())) {String pm = Xp.nexttext (); CITY.SETPM (PM); } break;
case XmlPullParser.END_TAG:if (' City '. Equals (Xp.getname ())) {Citys.add (city); }
//get the resource file to the SRC folderInputStream is = getClassLoader (). getResourceAsStream ("Weather.xml"); //get the Pull parser objectXmlpullparser XP =Xml.newpullparser (); //Initialize Try{xp.setinput (IS,"GBK"); //gets the event type of the current node, with the event type's judgment, we can know what node the current node is, thus determining what we should do intType =Xp.geteventtype (); City City=NULL; while(Type! =xmlpullparser.end_document) { //depending on the type of node, different operations are done Switch(type) { CaseXmlpullparser.start_tag://gets the name of the current node if("Weather". Equals (Xp.getname ())) { //Create a City collection object to hold the city's JavaBeanCityList =NewArraylist<city>(); } Else if("City". Equals (Xp.getname ())) { //Create a JavaBean object for CityCity =NewCity (); } Else if("Name". Equals (Xp.getname ())) { //gets the text of the next node of the current nodeString name =Xp.nexttext (); City.setname (name); } Else if("Temp". Equals (Xp.getname ())) { //gets the text of the next node of the current nodeString temp =Xp.nexttext (); City.settemp (temp); } Else if("PM". Equals (Xp.getname ())) { //gets the text of the next node of the current nodeString pm =Xp.nexttext (); CITY.SETPM (PM); } Break; CaseXmlpullparser.end_tag:if("City". Equals (Xp.getname ())) { //put the city's JavaBean in the collectionCitylist.add (city); } Break; } //move the pointer to the next node and return the event type for that nodeType =Xp.next (); } for(city c:citylist) {System.out.println (c.tostring ()); } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }
Pull Parse XML file