从任意数据结构生成XML解析器产生SAX事件(1)
文章作者 100test 发表时间 2007:03:14 16:28:55
来源 100Test.Com百考试题网
在j2ee1.4标准教材里看到一个很有趣的例子,从任意数据结构生成XML解析器产生SAX事件.数据结构可以是文本文件,PDF格式文档等.关键是自己解析这些数据源.另外一个有意思的地方是观察者模式的应用.所以就粗糙的改了一下并完整到可以测试运行.观察者模式简略UML图:
具体实现 被观察者对象ParseXMLSubject类:
package test.
import java.io.*.
import org.xml.sax.helpers.AttributesImpl.
import org.xml.sax.*.
public class ParseXMLSubject implements XMLReader {
ContentHandler handler.
String nsu = "".
Attributes atts = new AttributesImpl().
String rootElement = "addressbook".
String indent = "\n ".
public ParseXMLSubject(){
}
public ContentHandler getContentHandler() {
return handler.
}
public void parse(InputSource input) throws IOException, SAXException {
try {
// Get an efficient reader for the file
java.io.Reader r = input.getCharacterStream().
BufferedReader br = new BufferedReader(r).
// Read the file and display it s contents.
String line = br.readLine().
while (null != (line = br.readLine())) {
if (line.startsWith("email:")) {
break.
}
}