我想解析我的xml,如下所示。 其實我試圖調試它,所以控制不會去startElement()
方法中的那個School標籤。爲什麼我的xml不能正確解析?
<response>
<school>
<School>
<id>1</id>
<school_name>ABC</school_name>
<logo/>
<phone>+91-9764239061</phone>
</School>
</school>
</response>
,我的代碼是:
私有靜態最後絃樂TAG = SchoolParser.class.getName()的toString()。 private list schoolListData; private boolean isSuccess; 私立學校; 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); } }
}