用SAX(JDK自带API)解析XML文件
解析的工具类继承org.xml.sax.helpers.DefaultHandler,然后覆盖父类的几个方法即可。方法的详细解释可以参照JDK的API文档。
- startDocument() 开始解析XML时被调用,一般可以用来做初始化操作。
- startElement() 解析到某个元素(标签)的开头时(例如<name>)被调用,一般用来判断是否已开始解析某特定元素(标签)。
- endElement() 解析完某个元素(标签)时(例如</name>)被调用,一般在此对某些逻辑标记做重置操作。
- characters() 解析到某个文本元素(例如<name>张三</name>)时被调用,一般在此方法中取值,需要结合startElement()方法中设置的逻辑标志进行判断是否解析到XML文档中特定的位置。
下面是一个实例:
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<String> 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<String> getUrlPatternList() {
return urlPatternList;
}
@Override
public void startDocument() throws SAXException {
this.urlPatternList = new ArrayList<String>();
}
@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("<属性名>")获取到。