-1

I want to parse my xml as below. Actually I am trying to debug it, so control is not going to that School tag in the startElement() method.

<response>
 <school>
   <School>
      <id>1</id>
      <school_name>ABC</school_name>
      <logo/>
      <phone>+91-9764239061</phone>
   </School>
 </school>
</response>
  • and my code is :

    private static final String TAG = SchoolParser.class.getName().toString(); private List schoolListData ; private boolean isSuccess; private School school; private StringBuffer buffer;

    private boolean debug = true;
    
    
    public List<School> getSchoolListData(){
        return schoolListData;
    }
    
    
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        schoolListData = new ArrayList<School>();
    }
    
    
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        Log.d("data in School Parser", "Inside");
        if(localName.equals("School")){
            printInfoLog("New school======>");
            school = new School();
    
        }else if(localName.equals("id")){
            Log.d("data in School Parser inside Localin", "Inside Local");
    
        }else if(localName.equals("school_name")){
    
        }else if(localName.equals("logo")){
    
        }else if(localName.equals("phone")){
    
        }
    
        Log.d("data in School Parser inside Localout", "Inside Local");
    
    }
    
    
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        super.characters(ch, start, length);
        buffer.append(new String(ch, start, length));
        printInfoLog(buffer.toString());
    }
    
    
    
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        super.endElement(uri, localName, qName);
    
        if(localName.equals("School")){
            printInfoLog("Add New School======>");
            schoolListData.add(school);
            school =null;
        }else if(localName.equals("id")){
            school.setId(Integer.parseInt(buffer.toString().trim()));
        }else if(localName.equals("school_name")){
            school.setSchoolName(buffer.toString().trim());
        }else if(localName.equals("logo")){
            school.setLogo(buffer.toString().trim().getBytes());
        }else if(localName.equals("phone")){
            school.setPhn_no(buffer.toString().trim());
        }
        //      int size = buffer.length(); 
        //      buffer.delete(0, size);
        Log.i("buffer is empty", ""+buffer.toString());
    }
    
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
        isSuccess = false;
    }
    
    private void printInfoLog(String msg){
        if(debug ){
            Log.i(TAG, msg);
        }
    }
    

    }

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170

1 Answers1

0

change this

if(localName.equals("School")){
    printInfoLog("Add New School======>");
    schoolListData.add(school);
    school =null;
}else if(localName.equals("id")){
    school.setId(Integer.parseInt(buffer.toString().trim()));
}else if(localName.equals("school_name")){
    school.setSchoolName(buffer.toString().trim());
}else if(localName.equals("logo")){
    school.setLogo(buffer.toString().trim().getBytes());
}else if(localName.equals("phone")){
    school.setPhn_no(buffer.toString().trim());
}

to

if(qName.equals("School")){
    printInfoLog("Add New School======>");
    schoolListData.add(school);
    school =null;
}else if(qName.equals("id")){
    school.setId(Integer.parseInt(buffer.toString().trim()));
}else if(qName.equals("school_name")){
    school.setSchoolName(buffer.toString().trim());
}else if(qName.equals("logo")){
    school.setLogo(buffer.toString().trim().getBytes());
}else if(qName.equals("phone")){
    school.setPhn_no(buffer.toString().trim());
}