上次是用DOM读取的XML文件,我接着书往下读又发现了用SAX读取文件的方法,当然是用Groovy写的,我把它改为用java写的了,代码如下:
package test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
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;
publicclass JSAX {
publicstaticvoid main(String[] args) {
DefaultHandler handler = new PlanHandler();
try {
XMLReader reader = SAXParserFactory.newInstance().newSAXParser()
.getXMLReader();
reader.setContentHandler(handler);
FileInputStream inpustStream = new FileInputStream(
"./src/data/pla.xml");
reader.parse(new InputSource(inpustStream));
inpustStream.close();
System.out.println(((PlanHandler) handler).getUnderway());
System.out.println(((PlanHandler) handler).getUpcoming());
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class PlanHandler extends DefaultHandler {
List underway = new ArrayList();
List upcoming = new ArrayList();
@Override
publicvoid startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if (!name.equals("task"))
return;
String title = attributes.getValue("title");
String total = attributes.getValue("total");
String done = attributes.getValue("done");
switch (Integer.valueOf(done)) {
case 0:
upcoming.add(title);
break;
default:
if (!total.equals(done))
underway.add(title);
}
}
public List getUnderway() {
returnunderway;
}
public List getUpcoming() {
returnupcoming;
}
}
输出结果是:
[use in current project]
[re-read DB chapter, use DB/XML combination]
从代码上来看,SAX比DOM复杂了一些,主要是要写一个处理器类PlanHandler用来处理xml文件,而且Sax好像还不能在树形数据结构中进行位置的操作。书上说SAX是Push-Style型的(推型?),现在在java里处理xml文件的趋势是用Pull-Style型的(拉型?),在这之中最普遍的分析器是叫StAX-based parsers,不过这个是在java6中有,我没有java6的开发包,只能看看书上的代码了。
下面是书上用Groovy写的代码:
import javax.xml.parsers.SAXParserFactory
import org.xml.sax.*
import org.xml.sax.helpers.DefaultHandler
class PlanHandler extends DefaultHandler {
def underway = []
def upcoming = []
void startElement(String namespace, String localName,
String qName, Attributes atts) {
if (qName != 'task') return
def title = atts.getValue('title')
def total = atts.getValue('total')
switch (atts.getValue('done')) {
case'0' : upcoming << title ; break
case { it != total } : underway << title ; break
}
}
}
def handler = new PlanHandler()
def reader = SAXParserFactory.newInstance()
.newSAXParser().xMLReader
reader.contentHandler = handler
def inputStream = new FileInputStream('data/plan.xml')
reader.parse(new InputSource(inputStream))
inputStream.close()
assert handler.underway == [
'use in current project'
]
assert handler.upcoming == [
're-read DB chapter',
'use DB/XML combination'
]
说实话,Groovy在至少在编写小程序上来说比java要方便很多。