上次是用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要方便很多。