contentdhandler.java

来自「java相关的j2me,j2se的一些相关资料」· Java 代码 · 共 82 行

JAVA
82
字号
import java.util.Hashtable;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ContentDHandler extends DefaultHandler{
    
    StringBuffer tagValue=null;
    String studentId;
    Hashtable student;
    Hashtable av;    //存放attr和value
    Hashtable attr;  //记录属性
    Hashtable tag;   //记录数据
    
    public ContentDHandler() {
        student = new Hashtable();
        av = new Hashtable();
        attr = new Hashtable();
        tag = new Hashtable();
    }
    
    public void startDocument() throws SAXException{
        System.out.println("开始读取文档 = "+Runtime.getRuntime().freeMemory());
    }
    
    public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {
        System.out.println("读取开始标签");
        if(qName.equals("class")){
            System.out.println("开始读取根节点!!!");
        }else if(qName.equals("student")){
            System.out.println("读取student节点,存储属性值");
            studentId = attributes.getValue("studentid");
            for(int i=0;i<attributes.getLength();i++){
                System.out.println("属性名 = "+attributes.getQName(i)+",属性值 = "+attributes.getValue(i));
                attr.put(attributes.getQName(i),attributes.getValue(i));
            }
        }else if(qName.equals("name")){
            System.out.println("读取name节点,存储属性值和值");
            for(int i=0;i<attributes.getLength();i++){
                System.out.println("属性名 = "+attributes.getQName(i)+",属性值 = "+attributes.getValue(i));
                attr.put(attributes.getQName(i),attributes.getValue(i));
            }
        }
    }
    
    public void characters(char[] ch,int start,int length) throws SAXException {
        tagValue = new StringBuffer("");
        tagValue.append(ch,start,length);
        System.out.println("标签值="+tagValue.toString());
    }
    
    public void endElement(java.lang.String uri,java.lang.String localName,java.lang.String qName) throws SAXException{
        System.out.println("读取结束标签");
        if(qName.equals("class")){
            System.out.println("读取结束根节点!!!");
        }else if(qName.equals("student")){
            System.out.println("读取结束student节点,存储值到student哈希表,studentId="+studentId);
            av.put("attr",attr);
            av.put("tag",tag);
            student.put(studentId,av);
            attr.clear();
            tag.clear();
            av.clear();
        }else{
            System.out.println("存储数据到tag哈希表,标签名="+qName+",值="+tagValue.toString());
            tag.put(qName,tagValue.toString());
        }
    }
    
    public void endDocument() throws SAXException{
        System.out.println("结束文档读取 = "+Runtime.getRuntime().freeMemory());
    }
    
    public void setDocumentLocator(Locator locator){
        System.out.println("无"+locator.getColumnNumber());
    }
    
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?