miércoles, 12 de junio de 2013

XML Parser (XmlPullParser)

I feel more confortable parsing JSON, but sometimes I get some services in XML. After reading some articles about parsing and reading the google advices I finally decided to use the class XMLPullParser, not bad, here an example.


First we get the xml
public InputStream getResponseInputStream(String url){
  
  Log.i("URL REQUEST",url);
  URL request;
  try {
   
   request = new URL(url); 
   HttpURLConnection  conn = (HttpURLConnection)request.openConnection(); 
   conn.setConnectTimeout(30000);
   conn.setReadTimeout(30000);
   
   InputStream is = conn.getInputStream();
   return is;

  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  return null;
 }

Create the parser
public List<Member> getMemberData(String url){
  
  InputStream is=null;
  try{
   is = getResponseInputStream(url);

   XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
         parser.setInput(is, null);
        
         parser.nextTag();
         List<Member> members = readMembers(parser);
   is.close();
   
   return members;
  
  } catch (Exception e) {
   e.printStackTrace();
   
  }
  
  return null;
 }
Parsing:
private List<Member> readMembers(XmlPullParser parser) throws XmlPullParserException, IOException {
     List<Member> entries = new ArrayList<Member>();

     parser.require(XmlPullParser.START_TAG, null, "root");
     while (parser.next() != XmlPullParser.END_TAG) {
         if (parser.getEventType() != XmlPullParser.START_TAG) {
             continue;
         }
         String name = parser.getName();
         // Starts by looking for the entry tag
         if (name.equals("item")) {
             entries.add(readMemberDetail(parser));
         } else {
             skip(parser);
         }
     }  
     return entries;
 }
 
 private Member readMemberDetail(XmlPullParser parser) throws XmlPullParserException, IOException {
     parser.require(XmlPullParser.START_TAG, null, "item");
     Member member = new Member();

     while (parser.next() != XmlPullParser.END_TAG) {
         if (parser.getEventType() != XmlPullParser.START_TAG) {
             continue;
         }
         
         String localName = parser.getName();
         
         if(localName.equals("id")) member.id = readText(parser,"id");
else if(localName.equals("name")) member.name = readText(parser,"name");
else if(localName.equals("address")) member.address = readText(parser,"address");
else skip(parser);

        
     }
     return member;
 }
And some utils like readText to get the content and skip if we get some tag we don't want to parse
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
     if (parser.getEventType() != XmlPullParser.START_TAG) {
         throw new IllegalStateException();
     }
     int depth = 1;
     while (depth != 0) {
         switch (parser.next()) {
         case XmlPullParser.END_TAG:
             depth--;
             break;
         case XmlPullParser.START_TAG:
             depth++;
             break;
         }
     }
  }
 
 public String readText(XmlPullParser parser, String tag){
  try{
   parser.require(XmlPullParser.START_TAG, null, tag);
      String text = readText(parser);
      parser.require(XmlPullParser.END_TAG, null, tag);
      return text;
  } catch (XmlPullParserException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  return null;
 }
More info

No hay comentarios:

Publicar un comentario