Android使用SAX解析XML(5)
parse_handler.java文件:
package com.hzhi.my_sax; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class parse_handler extends DefaultHandler{ private infor m_infor; private school m_c_school; private major m_c_major; public parse_handler(infor i){ m_infor = i; } public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException{ super.startElement(uri, localName, name, attributes); // 当前元素学院 if (localName.equalsIgnoreCase(school.tag_name)){ String school_name = attributes.getValue("Name"); String code = attributes.getValue("Code"); school m_school = new school(school_name, code); m_infor.add_school(m_school); // 记录当前的学院 m_c_school = m_school; } // 当前元素有专业 else if (localName.equalsIgnoreCase(major.tag_name)){ String major_name = attributes.getValue("Name"); String code = attributes.getValue("Code"); major m_major = new major(major_name, code); m_c_school.add_major(m_major); // 记录当前的学院 m_c_major = m_major; } // 当前元素有班级 else if (localName.equalsIgnoreCase(clas.tag_name)){ String clas_name = attributes.getValue("Name"); String code = attributes.getValue("Code"); //如果其从属的专业不为空则添加其专业的班级列表 if(m_c_major != null) { clas m_clas = new clas(clas_name, code); m_c_major.add_clas(m_clas); } } } }
该类的startElement()函数具体的完成了对XML文件的解析过程,并将解析结果放入变量m_infor中。