zoukankan      html  css  js  c++  java
  • 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中。

  • 相关阅读:
    yii2美化url
    Android 百度地图API 定位 导航
    辛星浅谈PHP的混乱的编码风格
    html5非常火,他究竟与html4有何差别?
    Cocos2d-x 3.0心得(01)-图片载入与混合模式
    解读BOM与COM
    HDU 1718 Rank counting sort解法
    汉语转拼音pinyin4j
    UVALive-6656-Watching the Kangaroo(二分)
    thinPHP中多维数组的遍历
  • 原文地址:https://www.cnblogs.com/mstk/p/3485530.html
Copyright © 2011-2022 走看看