📄 saxtest.java.svn-base
字号:
package sax;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;;
public class SaxTest extends DefaultHandler{
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
XMLReader xr=XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
xr.setContentHandler(new SaxTest());
xr.parse(new InputSource(new FileReader("export_yndw_all.xml")));
}
//在文档开始的时候调用此方法
public void startDocument() throws SAXException {
System.out.println("startDocument: ");
}
//在文档结束的时候调用此方法
public void endDocument() throws SAXException {
System.out.println("enddocument: ");
}
//在遇到开始标签时调用此方法,
//其中参数中的namespaceURI就是名域,localName是标签名,qName是标签的修饰前缀,当没有使用名域的时候,这两个参数都未null。而atts是这个标签所包含的属性列表。通过atts,可以得到所有的属性名和相应的值
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println("startElement: "+localName);
}
//遇到结束标签时调用此方法
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println("endElement: "+localName);
}
//当遇到标签中的字符串时,调用这个方法,它的参数是一个字符数组,以及读到的这个字符串在这个数组中的起始位置和长度
public void characters(char[] ch, int start, int length) throws SAXException {
String data=new String(ch,start,length);
System.out.println(data);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -