【问题标题】:Why my xml is not parsing properly?为什么我的 xml 解析不正确?
【发布时间】:2013-10-22 11:23:19
【问题描述】:

我想如下解析我的 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);
        }
    }
    

    }

【问题讨论】:

  • 1ABC+91-9764239061学校>

标签: android xml xml-parsing saxparser


【解决方案1】:

改变这个

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

【讨论】:

  • @user2890202 还检查是否也调用了 startElement :)
  • 它没有检查标签,这就是它给出 NullPointerException 的原因......,但我已经给出了正确的标签名称并正确比较了......
  • 不知道为什么,bt .parse 方法在哪里调用?参考this for xml parsing in android
  • 在我调用的其他类中,parser.parse(xmlStrem,new SchoolParser());
  • 主要问题是,它没有检测/比较标签..为什么会这样??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多