how to get values from same keyname from xml in android -


i have make demo parsing xml response in android,i have tried , goes well,but dont know how values of same argument having same keyname xml response,my sample xml below,

<nightlyratesperroom size="8"> <nightlyrate baserate="315.68" rate="315.68" promo="false"/> <nightlyrate baserate="229.33" rate="229.33" promo="false"/> <nightlyrate baserate="248.52" rate="248.52" promo="false"/> <nightlyrate baserate="267.71" rate="267.71" promo="false"/> <nightlyrate baserate="267.71" rate="267.71" promo="false"/> <nightlyrate baserate="248.52" rate="248.52" promo="false"/> <nightlyrate baserate="286.9" rate="286.9" promo="false"/> <nightlyrate baserate="286.9" rate="286.9" promo="false"/> </nightlyratesperroom> 

mycode

package com.amar.travelonwards.parser;  import java.io.ioexception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.url; import java.util.arraylist; import java.util.hashmap; import java.util.list;  import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserfactory;  import com.amar.travelonwards.model.rssfeed_roominfo; import com.amar.travelonwards.utility.consts; import com.amar.travelonwards.utility.hotelbookingdetailshandler;  public class newsfeedparser_roominfo {     private inputstream urlstream;     private xmlpullparserfactory factory;     private xmlpullparser parser;      private list<rssfeed_roominfo> rssfeedlist_room;      private rssfeed_roominfo rssfeed_room;      public list<string> room_info;     public list<string> bed_info;     public list<string> commissionlist;     public list<string> surchargelist;     public list<string> totalchargelist;     public list<string> cancellation;     public list<string> urllist;      arraylist<string> al;     private string urlstring;     private string tagname;      private string roomtypedescription;     private string bedtypedesc;      private string commissionableusdtotal;     private string surchargetotal;     private string total;     private string averagerate;      private string cancellationpolicy;     private string url;      private string ratecode;     private string roomtypecode;     private string suppliertype;     private string bedtype;     private string smokepref;      private string ratekey;      public list<string> values;      public list<string> list_ratecode;     public list<string> list_roomtypecode;     public list<string> list_suppliertype;     public list<string> list_bedtype;     public list<string> list_smokepref;     public list<string> list_ratekey;      public list<string> total_addvalue_list;      /*      * string temp[]; int i;      */      public list<list<string>> addvaluelist;     int = 0;     public static final string item = "hotelroomresponse";     public static final string channel = "ns2:hotelroomavailabilityresponse";      public static final string hotel_name = "roomtypedescription";     public static final string room_desc = "bedtype";      public static final string room_charge = "chargeablerateinfo";     public static final string cancellation = "cancellationpolicy";      public static final string url = "url";      public static final string hotel_id = "hotelid";     public static final string arrival_date = "arrivaldate";     public static final string departure_date = "departuredate";     public static final string rate_code = "ratecode";     public static final string room_type_code = "roomtypecode";     public static final string supplier_type = "suppliertype";     public static final string bed_type = "bedtype";     public static final string smoke_pref = "smokingpreferences";     public static final string rate_key = "ratekey";      public static final string value_adds = "valueadds";     public static final string value_add = "valueadd";      public hotelbookingdetailshandler bookingdetailshandler;      public newsfeedparser_roominfo(string urlstring) {         this.urlstring = urlstring;     }      public newsfeedparser_roominfo() {         // todo auto-generated constructor stub     }      public static inputstream downloadurl(string urlstring) throws ioexception {         url url = new url(urlstring);         httpurlconnection conn = (httpurlconnection) url.openconnection();         conn.setrequestmethod("get");         conn.setdoinput(true);         // conn.setdooutput(true);          conn.setrequestproperty("accept",                 "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");         conn.setrequestproperty("accept-language", "en-us,en;q=0.5");         conn.setrequestproperty("accept-charset",                 "iso-8859-1,utf-8;q=0.7,*;q=0.7");          conn.connect();          int rcode = conn.getresponsecode();          system.out.println("************responce code:************" + rcode);          inputstream stream = conn.getinputstream();          if (conn.getresponsecode() != 200) {             stream = conn.geterrorstream();             system.out.println("**********************ok**************");         } else {             stream = conn.getinputstream();         }          return stream;     }      public list<rssfeed_roominfo> parse() {         try {             int count = 0;             factory = xmlpullparserfactory.newinstance();             parser = factory.newpullparser();             urlstream = downloadurl(urlstring);             parser.setinput(urlstream, null);             int eventtype = parser.geteventtype();             boolean done = false;             rssfeed_room = new rssfeed_roominfo();              room_info = new arraylist<string>();             bed_info = new arraylist<string>();              commissionlist = new arraylist<string>();             surchargelist = new arraylist<string>();             totalchargelist = new arraylist<string>();             cancellation = new arraylist<string>();             urllist = new arraylist<string>();              addvaluelist = new arraylist<list<string>>();             total_addvalue_list = new arraylist<string>();              list_ratecode = new arraylist<string>();             list_roomtypecode = new arraylist<string>();             list_suppliertype = new arraylist<string>();             list_bedtype = new arraylist<string>();             list_smokepref = new arraylist<string>();             list_ratekey = new arraylist<string>();              rssfeedlist_room = new arraylist<rssfeed_roominfo>();             while (eventtype != xmlpullparser.end_document && !done) {                 tagname = parser.getname();                 // system.out.println("city tag:"+tagname);                 switch (eventtype) {                 case xmlpullparser.start_document:                      break;                 case xmlpullparser.start_tag:                      if (tagname.equals(item)) {                          rssfeed_room = new rssfeed_roominfo();                         values = new arraylist<string>();                     }                     if (tagname.equals(rate_code)) {                         ratecode = parser.nexttext().tostring();                          system.out.println("parsed ratecode:" + ratecode);                         list_ratecode.add(ratecode);                     }                     if (tagname.equals(room_type_code)) {                         roomtypecode = parser.nexttext().tostring();                          system.out.println("parsed roomtypecode:"                                 + roomtypecode);                         list_roomtypecode.add(roomtypecode);                     }                     if (tagname.equals(supplier_type)) {                         suppliertype = parser.nexttext().tostring();                          system.out.println("parsed suppliertype:"                                 + suppliertype);                         list_suppliertype.add(suppliertype);                     }                     if (tagname.equals(bed_type)) {                         bedtype = parser.getattributevalue(null, "id");                          system.out.println("parsed bedtype:" + bedtype);                         list_bedtype.add(bedtype);                     }                     if (tagname.equals(smoke_pref)) {                         smokepref = parser.nexttext().tostring();                          system.out.println("parsed smokepref:" + smokepref);                         list_smokepref.add(smokepref);                     }                     if (tagname.equals(rate_key)) {                         ratekey = parser.nexttext().tostring();                          system.out.println("parsed ratekey:" + ratekey);                         list_ratekey.add(ratekey);                     }                     //                      if (tagname.equals(hotel_name)) {                         roomtypedescription = parser.nexttext().tostring();                          system.out.println("parsed roomtypedescription:"                                 + roomtypedescription);                         room_info.add(roomtypedescription);                     }                     if (tagname.equals(room_desc)) {                         parser.nexttag();                         parser.require(xmlpullparser.start_tag, "",                                 "description");                         bedtypedesc = parser.nexttext();                         parser.require(xmlpullparser.end_tag, "", "description");                         bed_info.add(bedtypedesc);                         system.out.println("parsed bedtypedesc:" + bedtypedesc);                      }                     if (tagname.equals(room_charge)) {                         commissionableusdtotal = parser.getattributevalue(null,                                 "commissionableusdtotal");                         commissionlist.add(commissionableusdtotal);                         system.out                                 .println("parse commission commissionableusdtotal:"                                         + commissionableusdtotal);                          surchargetotal = parser.getattributevalue(null,                                 "surchargetotal");                         surchargelist.add(surchargetotal);                         system.out.println("parse surcharged surchargetotal:"                                 + surchargetotal);                          total = parser.getattributevalue(null, "total");                         totalchargelist.add(total);                         system.out.println("parsed total:" + total);                          averagerate = parser.getattributevalue(null,                                 "averagerate");                         system.out.println("print avgnight:" + averagerate);                         system.out.println("parsed commissionableusdtotal:"                                 + commissionableusdtotal);                     }                       if (tagname.equalsignorecase("nightlyratesperroom")) {                             system.out.println("<nightlyratesperroom>");                              al  = new arraylist<string>(); // if root tag, arraylist begins here                          }                          if (tagname.equalsignorecase("nightlyrate")) {                             system.out.print("<nightlyrate>");                             string ratevalue = parser.getattributevalue(null, "rate");                             system.out.print("rate : " + ratevalue);                             al.add(ratevalue); // adds value i.e rate al                         }                     if (tagname.equals(cancellation)) {                         cancellationpolicy = parser.nexttext().tostring();                         system.out.println("parsed cancellationpolicy:"                                 + cancellationpolicy);                         cancellation.add(cancellationpolicy);                     }                      if (tagname.equals(value_add)) {                          parser.nexttag();                         parser.require(xmlpullparser.start_tag, "",                                 "description");                         values.add(parser.nexttext());                         parser.require(xmlpullparser.end_tag, "", "description");                      }                     if (tagname.equals(url)) {                         url = parser.nexttext().tostring();                         system.out.println("parsed url:" + url);                         urllist.add(url);                     }                     break;                 case xmlpullparser.end_tag:                     if (tagname.equals(channel)) {                         done = true;                      } else if (tagname.equals(item)) {                          rssfeed_room = new rssfeed_roominfo(                                 roomtypedescription, bedtypedesc,                                 commissionableusdtotal, surchargetotal, total,                                 averagerate, cancellationpolicy, url, ratecode,                                 roomtypecode, suppliertype, bedtype, smokepref,                                 ratekey);                         consts.roomselectionlist.clear();                         hashmap<string, string> roommap = new hashmap<>();                          roommap.put("roomtypedescription", roomtypedescription);                         roommap.put("bedtypedesc", bedtypedesc);                         roommap.put("commissionableusdtotal",                                 commissionableusdtotal);                         roommap.put("surchargetotal", surchargetotal);                         roommap.put("total", total);                         roommap.put("averagerate", averagerate);                         roommap.put("cancellationpolicy", cancellationpolicy);                         roommap.put("url", url);                         roommap.put("ratecode", ratecode);                         roommap.put("roomtypecode", roomtypecode);                          roommap.put("suppliertype", suppliertype);                         roommap.put("bedtype", bedtype);                         roommap.put("smokepref", smokepref);                         roommap.put("ratekey", ratekey);                         consts.roomselectionlist.add(roommap);                         rssfeedlist_room.add(rssfeed_room);                          addvaluelist.add(values);                          system.out.println("addvalues:::" + addvaluelist);                      }                      break;                 }                 eventtype = parser.next();             }         } catch (exception e) {             e.printstacktrace();         }          return rssfeedlist_room;     } } 

i want values of "rate" in arraylist,can me figure out issue,thanks in advance.

it's simple. xml parsing, used xmlpullparser. can try code.

demo class

import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.util.arraylist;  import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserexception; import org.xmlpull.v1.xmlpullparserfactory;  public class demo {      public static void main(string[] args) throws xmlpullparserexception, ioexception     {       try {           xmlpullparserfactory factory = xmlpullparserfactory.newinstance();          arraylist<string> al = null; // arraylist initialized here          xmlpullparser xpp = factory.newpullparser();          file xmlfile = new file("test.xml");          fileinputstream fis = new fileinputstream(xmlfile);          xpp.setinput(fis, null);          xpp.setfeature(xmlpullparser.feature_process_namespaces, false);          int eventtype=xpp.geteventtype();           while(eventtype!=xmlpullparser.end_document){              string tagname = xpp.getname();                                   switch (eventtype) {                  case xmlpullparser.start_tag:                         if (tagname.equalsignorecase("nightlyratesperroom")) {                         system.out.println("<nightlyratesperroom>");                         al = new arraylist<string>(); // if root tag, arraylist begins here                      }                      if (tagname.equalsignorecase("nightlyrate")) {                         system.out.print("<nightlyrate>");                         string ratevalue = xpp.getattributevalue(null, "rate");                         system.out.print("rate : " + ratevalue);                         al.add(ratevalue); // adds value i.e rate al                     }                      break;                  case xmlpullparser.end_tag:                      if (tagname.equalsignorecase("nightlyratesperroom")) {                         system.out.println("</nightlyratesperroom>");                     }                       if (tagname.equalsignorecase("nightlyrate")) {                                               system.out.println("</nightlyrate>");                                            }                  default:                     break;              }               eventtype=xpp.next();          }         system.out.println("**** arraylist values starts printing ****");           (int = 0; < al.size(); i++) {               system.out.println("rate : " +al.get(i));          } // end of loop       } // end of try             catch (xmlpullparserexception e) {                 e.printstacktrace();             } catch (ioexception e) {                 e.printstacktrace();              }      } // end of main function  } // end of class 

output :

<nightlyratesperroom> <nightlyrate>rate : 315.68</nightlyrate> <nightlyrate>rate : 229.33</nightlyrate> <nightlyrate>rate : 248.52</nightlyrate> <nightlyrate>rate : 267.71</nightlyrate> <nightlyrate>rate : 267.71</nightlyrate> <nightlyrate>rate : 248.52</nightlyrate> <nightlyrate>rate : 286.9</nightlyrate> <nightlyrate>rate : 286.9</nightlyrate> </nightlyratesperroom> **** arraylist values starts printing **** rate : 315.68 rate : 229.33 rate : 248.52 rate : 267.71 rate : 267.71 rate : 248.52 rate : 286.9 rate : 286.9 

test.xml

<?xml version="1.0" encoding="utf-8"?> <nightlyratesperroom size="8"> <nightlyrate baserate="315.68" rate="315.68" promo="false"/> <nightlyrate baserate="229.33" rate="229.33" promo="false"/> <nightlyrate baserate="248.52" rate="248.52" promo="false"/> <nightlyrate baserate="267.71" rate="267.71" promo="false"/> <nightlyrate baserate="267.71" rate="267.71" promo="false"/> <nightlyrate baserate="248.52" rate="248.52" promo="false"/> <nightlyrate baserate="286.9" rate="286.9" promo="false"/> <nightlyrate baserate="286.9" rate="286.9" promo="false"/> </nightlyratesperroom>       

note : use xmlpullparser, need download library , add in project.

to work in android, copy , paste working code try , paste in class. hope helps!


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -