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中。

  • 相关阅读:
    046.Kubernetes集群管理-日常运维
    045.Kubernetes集群存储-CSI存储机制
    044.Kubernetes集群存储-StorageClass
    043.Kubernetes集群存储-共享存储
    CKAD考试心得分享
    050.Kubernetes集群管理-Prometheus+Grafana监控方案
    附015.Kubernetes其他技巧
    041.Kubernetes集群网络-K8S网络策略
    042.Kubernetes集群网络-flannel及calico
    040.Kubernetes集群网络-CNI网络模型
  • 原文地址:https://www.cnblogs.com/mstk/p/3485530.html
Copyright © 2011-2022 走看看