-1

以下のように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(); プライベート リスト schoolListData ; プライベート ブール値 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);
        }
    }
    

    }

4

1 に答える 1

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());
}
于 2013-10-22T12:00:42.737 に答える