Monday, 12 August 2013

Plist parsing


  private String getEventsFromAnPList(Activity activity,String xmlFilePath) throws XmlPullParserException, IOException
{
        
   boolean isInnnerList=false;
  StringBuffer stringBuffer = new StringBuffer();
  //Resources res = activity.getResources();
   //XmlResourceParser xpp = res.getXml(R.xml.myplist);
 File file = new File (xmlFilePath);
 FileInputStream fis = new FileInputStream(file);
 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
 factory.setNamespaceAware(true);
 XmlPullParser xpp = factory.newPullParser();
 xpp.setInput(fis, "UTF-8");
  xpp.next();
           int eventType = xpp.getEventType();
           int flag1 = 0, flag2 = 0, flag3 = 0;
           String tagName="";
           String i_key="",i_value="";
           WordInnerLessonBean wordInnerLessonBean = null;
           while (eventType != XmlPullParser.END_DOCUMENT)
           {
               if(eventType == XmlPullParser.START_TAG)
               {
                
                tagName="<"+xpp.getName().toString()+">";
                   String mainTag = xpp.getName().toString();
                   if(mainTag.equals("dict"))
                   {
                       if(flag1==0)
                       {
                       flag1 = 1;
                       i=i+1;
                       stringBuffer.append("\n--MAIN DICTIONARY STARTS HERE---Number := "+i+"\n");
                       }
                       else
                       {
                           stringBuffer.append("\n\n------------INNER DICTIONARY STARTS HERE----------\n");
                           isInnnerList=true;
                           flag2 = 1;
                           wordInnerLessonBean = new WordInnerLessonBean();
                       }

                   }
                   
                   else if(mainTag.equals("key"))
                   {
                       flag3 = 1;
                   }
                   
                   else
                   {
                       flag3 = 2;
                   }
               }else if(eventType == XmlPullParser.END_TAG){
                   String g = xpp.getName().toString();
                   
                   if(g.equals("dict"))
                   {
                       if(flag1==1 && flag2!=1)
                       {
                          stringBuffer.append("\n\n------------MAIN DICTIONARY ENDS HERE----------\n\n\n");
                        flag1=0;
                       }
                       else
                       {   
                        //add to inner list wordInnerLessonBean;
                        wordLessonBean.setLesson(wordInnerLessonBean);
                        
                           stringBuffer.append("\n\n-----INNER DICTIONARY ENDS----------\n");
                           isInnnerList=false;
                           flag2=0;
                       }
                   }
               } else if(eventType == XmlPullParser.TEXT) {
                
                   if(flag3 == 1)
                   {
                       stringBuffer.append("\nkey:="+xpp.getText());
                       i_key=xpp.getText();
                       
                       flag3 = 0;
                   }
                   else if(flag3 == 2)
                   {
                    
                       
                       flag3 = 0;
                       //value is zero then store tag name as a value 
                       if(xpp.getText().trim().length()<=0){
                        stringBuffer.append("\nValue:="+tagName);
                        i_value = tagName;
                       }else{
                         stringBuffer.append("\nValue:="+xpp.getText());
                         i_value = xpp.getText();
                       }
                       if(i_key.equals("audioDescFile")){
                        if(i_value.equals("<string>")){
                         wordLessonBean.setAudioDescFile("");
                        }else{
                         wordLessonBean.setAudioDescFile(i_value);
                        }
                        
                       }else if(i_key.equals("captureAudio")){
                        if(i_value.equals("<true>")){
                         wordLessonBean.setCaptureAudio(true);
                        }else{
                         wordLessonBean.setCaptureAudio(false);
                        }
                        System.out.println("in adapter captureAudio"+i_value.equals("<true>"));
                       }else if(i_key.equals("desc")){
                        wordLessonBean.setDesc(i_value);
                       }else if(i_key.equals("emailOnCompletion")){
      
                       }else if(i_key.equals("lessonType")){
                        
                       }else if(i_key.equals("name")){
                           wordLessonBean.setName(i_value);
                       }else if(i_key.equals("nextWordID")){
                        
                       }else if(i_key.equals("words")){
                        
                       }
                       
                       if(isInnnerList){
                        
                        if(i_key.equals("shapeTraceImage")){
                                  if(i_value.equals("<string>")){
                              wordInnerLessonBean.setShapeTraceImage("");
                             }else{
                              wordInnerLessonBean.setShapeTraceImage(i_value);
                             }
                           }else if(i_key.equals("word")){
                            
                            if(i_value.equals("<string>")){
                             wordInnerLessonBean.setWord("");
                            }else{
                             wordInnerLessonBean.setWord(i_value);
                            }
                           }else if(i_key.equals("wordAudio")){
                            if(i_value.equals("<string>")){
                             wordInnerLessonBean.setWordAudio("");
                            }else{
                             wordInnerLessonBean.setWordAudio(i_value);
                            }
                            
                           }else if(i_key.equals("wordID")){
                            wordInnerLessonBean.setWordID(Integer.parseInt(i_value));
                           }else if(i_key.equals("wordPicture")){
                            if(i_value.equals("<string>")){
                             wordInnerLessonBean.setWordPicture("");
                            }else{
                             wordInnerLessonBean.setWordPicture(i_value);
                            }
                           
                           }else if(i_key.equals("wordText")){
                            if(i_value.equals("<string>")){
                             wordInnerLessonBean.setWordText("");
                            }else{
                             wordInnerLessonBean.setWordText(i_value);
                            }
                           }
                       }
                      // System.out.println("isInnnerList:"+isInnnerList+" i_value:"+i_value);
                   }
               }
               
                eventType = xpp.next();
             }
           stringBuffer.append("\n--- End XML ---");
           stringBuffer.append("\n--- Total Number Of Dictionaries Are ---"+i);
           
           return stringBuffer.toString();
       }

No comments:

Post a Comment