扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年9月22日
关键字:
在本页阅读全文(共2页)
public ErrorHandler getErrorHandler() {
return null;
}
public boolean getFeature(String name) throws SAXNotRecognizedException,
SAXNotSupportedException {
return false;
}
public Object getProperty(String name) throws SAXNotRecognizedException,
SAXNotSupportedException {
return null;
}
public void setContentHandler(ContentHandler handler) {
this.handler = handler;
}
public void setDTDHandler(DTDHandler handler) {
}
public void setEntityResolver(EntityResolver resolver) {
}
public void setErrorHandler(ErrorHandler handler) {
}
public void setFeature(String name, boolean value) throws
SAXNotRecognizedException, SAXNotSupportedException {
}
public void setProperty(String name, Object value) throws
SAXNotRecognizedException, SAXNotSupportedException {
}
void output(String name, String line) throws SAXException {
int tmp = name.length();
int startIndex=tmp+1;
int textLength = line.length() - startIndex;
String characters = line.substring(startIndex,line.length()-1);
handler.ignorableWhitespace(indent.toCharArray(), 0, // start index
indent.length());
handler.startElement(nsu, name, name /*"qName"*/, atts);
handler.characters(characters.toCharArray(), startIndex, textLength);
handler.endElement(nsu, name, name);
}
}
具体观察者对象: ConcreateObserver类
package test;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
public class ConcreateObserver extends DefaultHandler {
public ConcreateObserver() {
}
public void startElement(String uri,
String localName,
String qName,
Attributes attributes)
throws SAXException{
System.out.println("startElement: "+localName);
}
public void characters(char[] ch,
int start,
int length)
throws SAXException{
System.out.println("characters: ");
System.out.print(ch);
System.out.println();
}
}
测试类:TestMain
package test;
import java.io.*;
import org.xml.sax.InputSource;
public class TestMain {
public TestMain() {
}
public static void main(String[] args) throws Exception {
TestMain testmain = new TestMain();
FileReader in = new FileReader(new File("d:\\AddressBookReaderLog01.txt"));
ConcreateObserver observer=new ConcreateObserver();
ParseXMLSubject parse = new ParseXMLSubject();
parse.setContentHandler(observer);
parse.parse(new InputSource(in));
}
}
测试文本文档:AddressBookReaderLog01.txt
AddressBookReader01 ../samples/PersonalAddressBook.ldif
nickname: Fred
email: fred@barneys.house
html: TRUE
firstname: Fred
lastname: Flintstone
work: 999-Quarry
home: 999-BedrockLane
fax: 888-Squawk
pager: 777-pager
cell: 555-cell
另外一个也比较也有意思的地方就是具体观察者类从DefaultHandler 继承,该类是缺省适配器模式的应用.
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者