为什么我的xml没有正确解析?

时间:2013-10-22 11:23:19

标签: android xml xml-parsing saxparser

我想解析我的xml,如下所示。 实际上我正在尝试调试它,因此控制不会转到startElement()方法中的School标记。

<response>
 <school>
   <School>
      <id>1</id>
      <school_name>ABC</school_name>
      <logo/>
      <phone>+91-9764239061</phone>
   </School>
 </school>
</response>
  • 我的代码是:

    private static final String TAG = SchoolParser.class.getName()。toString();     private List schoolListData;     private boolean isSuccess;     私立学校;     私有StringBuffer缓冲区;

    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);
        }
    }
    

    }

1 个答案:

答案 0 :(得分:0)

更改此

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());
}

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());
}