Android 为什么我的xml解析不正确?
我想如下解析我的xml。 实际上,我正在尝试调试它,所以控件不会指向Android 为什么我的xml解析不正确?,android,xml,xml-parsing,saxparser,Android,Xml,Xml Parsing,Saxparser,我想如下解析我的xml。 实际上,我正在尝试调试它,所以控件不会指向startElement()方法中的School标记 <response> <school> <School> <id>1</id> <school_name>ABC</school_name> <logo/> <phone>+91-9764239061</ph
startElement()
方法中的School标记
<response>
<school>
<School>
<id>1</id>
<school_name>ABC</school_name>
<logo/>
<phone>+91-9764239061</phone>
</School>
</school>
</response>
1.
基础知识
+91-9764239061
- 我的代码是:
私有静态最终字符串标记=SchoolParser.class.getName().toString();
私有列表学校列表数据;
私有布尔访问;
私立学校;
私有字符串缓冲区
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); } }
}private boolean debug=true; 公共列表getSchoolListData(){ 返回学校列表数据; } @凌驾 public void startDocument()引发异常{ super.startDocument(); schoolListData=newArrayList(); } @凌驾 public void startElement(字符串uri、字符串localName、字符串qName、, 属性)引发SAX异常{ startElement(uri、localName、qName、属性); Log.d(“学校解析器中的数据”,“内部”); if(localName.equals(“学校”)){ 打印信息日志(“新学校====>”; 学校=新学校(); }else if(localName.equals(“id”)){ Log.d(“学校解析器中的数据在Localin内部”、“在Local内部”); }else if(localName.equals(“学校名称”)){ }else if(localName.equals(“logo”)){ }else if(localName.equals(“phone”)){ } Log.d(“数据在学校解析器内部Localout”,“内部Localout”); } @凌驾 公共无效字符(字符[]ch,整数开始,整数长度) 抛出SAX异常{ 超级字符(ch、开始、长度); append(新字符串(ch,start,length)); printInfoLog(buffer.toString()); } @凌驾 公共void endElement(字符串uri、字符串localName、字符串qName) 抛出SAX异常{ super.endElement(uri、localName、qName); if(localName.equals(“学校”)){ printInfoLog(“添加新学校=====>”; schoolListData.add(学校); 学校=空; }else if(localName.equals(“id”)){ setId(Integer.parseInt(buffer.toString().trim()); }else if(localName.equals(“学校名称”)){ school.setSchoolName(buffer.toString().trim()); }else if(localName.equals(“logo”)){ setLogo(buffer.toString().trim().getBytes()); }else if(localName.equals(“phone”)){ school.setPhn_no(buffer.toString().trim()); } //int size=buffer.length(); //删除(0,大小); Log.i(“缓冲区是空的,”“+buffer.toString()); } @凌驾 public void endDocument()引发异常{ super.endDocument(); isSuccess=false; } 私有void打印信息日志(字符串msg){ 如果(调试){ Log.i(标签、消息); } }
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());
}
到
1 ABC+91-97642390061@user2890202还检查startElement是否也被调用:)它没有检查标记这就是为什么它给出了NullPointerException…,但我已经给出了正确的标记名并进行了正确的比较…我不确定为什么,bt调用的.parse方法在哪里?请参阅我调用的另一个类parser.parse(xmlStrem,newschoolparser());主要问题是,它没有检测/比较标签。为什么会这样??
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());
}