myhandler.java

来自「java_structs java web j架构分析java_structs 」· Java 代码 · 共 43 行

JAVA
43
字号
package xmlparse;

import org.xml.sax.helpers.*;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

public class MyHandler extends DefaultHandler {
    //XML开始部分调用该方法
    public void startDocument() throws SAXException {
        System.out.println("XML文档开始...........");
    }

    //XMl文件结束
    public void endDocument() throws SAXException {
        System.out.println("XML文档结束..........");
    }

    //读到文本对象的时候 调用
    public void characters(char ch[], int start, int length) throws
            SAXException {
        String s = new String(ch, start, length);
        if (!s.trim().equals(""))
            System.out.println(s);
    }

    //读到元素的时候 调用
    public void startElement(String uri, String localName,
                             String qName, Attributes attributes) throws
            SAXException {
        //元素的名字
        System.out.print(qName);
        //属性的名字和内容
        for(int i=0;i<attributes.getLength();i++){
          System.out.print (" " + attributes.getQName(i) +
                            "=" + attributes.getValue(i));
        }
        System.out.println();

    }


}

⌨️ 快捷键说明

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