?? mydefaulthandler.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 + -