用SAX(JDK自帶API)解析XML文件
解析的工具類繼承org.xml.sax.helpers.DefaultHandler,然后覆蓋父類的幾個方法即可。方法的詳細解釋可以參照JDK的API文檔。
- startDocument() 開始解析XML時被調用,一般可以用來做初始化操作。
- startElement() 解析到某個元素(標簽)的開頭時(例如
)被調用,一般用來判斷是否已開始解析某特定元素(標簽)。 - endElement() 解析完某個元素(標簽)時(例如)被調用,一般在此對某些邏輯標記做重置操作。
- characters() 解析到某個文本元素(例如
張三 )時被調用,一般在此方法中取值,需要結合startElement()方法中設置的邏輯標志進行判斷是否解析到XML文檔中特定的位置。
下面是一個實例:
Java代碼:
- import java.util.ArrayList;
- import java.util.List;
- import java.io.File;
- import java.io.IOException;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- public class URLPatternSAXParser extends DefaultHandler{
- private List
urlPatternList; - private boolean isSecurityConstraint;
- private boolean isWebResourceCollection;
- private boolean isUrlPattern;
- private final String elementSecurityConstraint = "security-constraint";
- private final String elementWebResourceCollection = "web-resource-collection";
- private final String elementUrlPattern = "url-pattern";
- public List
getUrlPatternList() { - return urlPatternList;
- }
- @Override
- public void startDocument() throws SAXException {
- this.urlPatternList = new ArrayList
(); - }
- //原文參考自杭州紅木家具維修http://www.hzlxwx.com
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if (elementSecurityConstraint.equals(qName)) {
- isSecurityConstraint = true;
- return;
- }
- if (isSecurityConstraint && elementWebResourceCollection.equals(qName)) {
- isWebResourceCollection = true;
- return;
- }
- if (isSecurityConstraint && isWebResourceCollection
- && elementUrlPattern.equals(qName))
- isUrlPattern = true;
- }
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- if (elementSecurityConstraint.equals(qName)) {
- isSecurityConstraint = false;
- }
- if (elementWebResourceCollection.equals(qName))
- isWebResourceCollection = false;
- if (elementUrlPattern.equals(qName))
- isUrlPattern = false;
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- if (isSecurityConstraint && isWebResourceCollection && isUrlPattern) {
- this.urlPatternList.add(new String(ch, start, length));
- }
- }
- public void parser(File xmlFile) throws SAXException, IOException,
- ParserConfigurationException {
- if (xmlFile == null) {
- throw new IllegalArgumentException(
- "parameter 'xmlFile' must not null !");
- }
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser parser = factory.newSAXParser();
- // 禁止DTD解析,避免因找不到DTD文件導致解析失敗。
- parser.getXMLReader()
- .setFeature(
- "http://apache.org/xml/features/nonvalidating/load-external-dtd",
- false);
- try {
- parser.parse(xmlFile, this);
- } catch (SAXException e) {
- System.err.println("Cann't parse " + xmlFile.getAbsolutePath());
- throw e;
- }
- }
- }
上面代碼中,parser()是主方法,調用完此方法之后,就可以調用getUrlPatternList()方法獲取到從web.xml中解析出來的url-pattern節點的值(security-constraint/web-resource-collection/url-pattern)。
另外,取元素的屬性值,可以在startElement()方法中用attributes.getValue("<屬性名>")獲取到。