⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydefaulthandler.java

📁 大家知道对于大XML文档,基于事件模型驱动的SAX,一个SAX解析XML的实例
💻 JAVA
字号:
package com.javausr.saxexample;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import com.lonton.aml.dealnewline.Assist;

import java.io.*;
import java.util.HashMap;
public class MyDefaultHandler extends DefaultHandler {   
private StringBuffer buf;
HashMap msg=new HashMap();
public void startDocument() throws SAXException {
buf=new StringBuffer();
System.out.println("*******开始解析文档*******");
}
public void endDocument() throws SAXException {
System.out.println("*******解析文档结束*******");
}
/*public void startPrefixMapping( String prefix, String uri ) {
System.out.println("\n前缀映射: " + prefix +" 开始!"+ "  它的URI是:"+uri);    
} 
public void endPrefixMapping( String prefix ) {
System.out.println("\n前缀映射: "+prefix+" 结束!");
}*/
public void startElement( String namespaceURI,String localName,String fullName, Attributes attributes )  throws SAXException {
System.out.println("\n元素: " + "["+fullName+"]" +" 开始解析!"); // 打印出属性信息       
for ( int i = 0; i < attributes.getLength(); i++ ) {System.out.println("\t属性名称:" + attributes.getLocalName(i)                + " 属性值:" + attributes.getValue(i));       
}    }   
public void endElement( String namespaceURI, String localName, String fullName )throws SAXException { //打印出非空的元素内容并将StringBuffer清空
String nullStr="";
if (!buf.toString().trim().equals(nullStr)){
System.out.println("\t内容是: " + buf.toString().trim());
}
buf.setLength(0); //打印元素解析结束信息
System.out.println("元素: "+"["+fullName+"]"+" 解析结束!");
if(fullName.equals("ERFL")){
	System.out.println("文件结束开始写文本");
}
} 
public void characters( char[] chars, int start, int length )throws SAXException { //将元素内容累加到StringBuffer中 
buf.append(chars,start,length);
}
/*public void warning( SAXParseException exception ){ 
System.out.println("*******WARNING******");
System.out.println("\t行:\t" + exception.getLineNumber());
System.out.println("\t列:\t" + exception.getColumnNumber());
System.out.println("\t错误信息:\t" + exception.getMessage());
System.out.println("********************");    }
public void error( SAXParseException exception ) throws SAXException{
System.out.println("******* ERROR ******");
System.out.println("\t行:\t" + exception.getLineNumber());
System.out.println("\t列:\t" + exception.getColumnNumber());
System.out.println("\t错误信息:\t" + exception.getMessage());
System.out.println("********************");
}
public void fatalError( SAXParseException exception ) throws SAXException {
System.out.println("******** FATAL ERROR ********");
System.out.println("\t行:\t" + exception.getLineNumber());
System.out.println("\t列:\t" + exception.getColumnNumber());
System.out.println("\t错误信息:\t" + exception.getMessage());       
System.out.println("*****************************");    
}*/
private void writeMsg(String strFilePath, StringBuffer sbMsg) {
    Assist IOReport = new Assist();
    boolean blnIO = IOReport.saveDataToFile(strFilePath, sbMsg.toString());
    if (!blnIO) {
        System.err.println("出错:输出到文件时出现错误.");
    }
}
}

⌨️ 快捷键说明

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