SAX在Java和Android中都已经集成,不需要再添加jar包。
使用SAX解析XML文件需要继承DefaultHandler类,并实现相应的方法。其中的方法有startDocument()、startElement()、characters()、endElement()、endDocument(),可以根据自己的需要进行实现。
注意:如果SAX在解析XML过程中需要停止解析,需要使用抛出异常才能终止。
方法的执行步骤如下:
<schools><!-- startDocument()->startElement()获得schools-->
<school><!-- characters()->startElement()获得school -->
<key>1</key><!--characters()->startElement()获得key->characters()获得1->endElement()获得key-->
<value>河南大学</value><!--characters()->startElement()获得value->characters()获得”河南大学“->endElement()获得value-->
</school><!-- characters()->endElement()获得school -->
…………
</schools><!-- characters()->endElement()获得schools->endDocument() -->
XmlPull在Android中已经集成,在Java中需要添加kxml.jar和xmlpull.jar的jar包。XmlPull解析的代码比较简单,容易控制停止解析。
XmlPull的jar包,请到git上下载:https://github.com/MasonYyp/XmlPull-s-jar-package
2.1.1 ParseXmlHandler.java源码
package parseXML;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ParseXmlHandler extends DefaultHandler{
private String currentTag = null;
private String currentKey = null;
private String currentValue = null;
private String schoolKey = null;
private String schoolValue = null;
// 初始化要查找的key
public ParseXmlHandler(String key) {
this.schoolKey = key;
}
// 获得学校的名字
public String getschool() {
return this.schoolValue;
}
// 开始读文件
@Override
public void startDocument() throws SAXException {
super.startDocument();
}
// 开始读节点
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
// 获得当前节点的名字
this.currentTag = qName;
// 有的使用的是localName,如果qName是空值的时候,可以试试使用localName
// this.currentTag = localName;
}
// 读节点中的内容
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
// 获得节点中的值
String value = new String(ch, start, length).trim();
if(!value.equals("")) {
if("key".equals(this.currentTag)) {
this.currentKey = value;
}else if("value".equals(this.currentTag)) {
this.currentValue = value;
}
}
}
// 读节点结束
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
//注意:如果qName是空值时,试试LocalName
if("school".equals(qName)) {
if(this.schoolKey.equals(this.currentKey)) {
this.schoolValue = this.currentValue;
// 抛出异常,停止查找
throw new SAXException("停止查找");
}else {
// 重置变量
this.currentTag = null;
this.currentKey = null;
this.currentValue = null;
}
}
}
// 结束读文档
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
}
package parseXML;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
public class ParseXmlUtils {
public String getDisease(String key) {
// 初始自定的Handler
ParseXmlHandler xmlHandler = new ParseXmlHandler(key);
try {
// 初始化SAX
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLReader xmlReader = factory.newSAXParser().getXMLReader();
xmlReader.setContentHandler(xmlHandler);
// 获得src目录项的文件
InputStream xmlFile = ParseXmlUtils.class.getClassLoader().getResourceAsStream("./xmlData/school.xml");
InputSource xmlInput = new InputSource(xmlFile);
// 开始解析xml文件
xmlReader.parse(xmlInput);
} catch (SAXException e) {
// 打印抛出的SAXException的异常
System.out.println(e.getMessage());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xmlHandler.getschool();
}
public static void main(String[] args) {
ParseXmlUtils utils = new ParseXmlUtils();
// 查找关键字是2的学学校
System.out.print(utils.getDisease("2"));
}
}
package parseXMLPull;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
public class ParseXMLPull {
public String getSchool(String schoolKey) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = factory.newPullParser();
// 读取XML文件
InputStream xmlFile = ParseXMLPull.class.getClassLoader().getResourceAsStream("./xmlData/school.xml");
InputStreamReader xmlReader = new InputStreamReader(xmlFile);
pullParser.setInput(xmlReader);
// 获得当前节点的事件类型
int eventType = pullParser.getEventType();
// 设置当前节点的值
String currentNodeName = null;
String currentKey = null;
String currentValue = null;
// 读取xml文件
while(eventType != pullParser.END_DOCUMENT) {
currentNodeName = pullParser.getName();
switch(eventType) {
// 开始解析节点
case XmlPullParser.START_TAG:{
// 获得相应的值
if("key".equals(currentNodeName)) {
currentKey = pullParser.nextText().trim();
}else if("value".equals(currentNodeName)) {
currentValue = pullParser.nextText().trim();
}
break;
}
// 结束解析节点
case XmlPullParser.END_TAG:{
if("school".equals(currentNodeName) && currentKey.equals(schoolKey)) {
// Return the school
return currentValue;
}
break;
}
// Break current node
default:
break;
}
// 继续读取下一节点
eventType = pullParser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
ParseXMLPull xmlPull = new ParseXMLPull();
System.out.println(xmlPull.getSchool("1"));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<schools><!-- startDocument()->startElement()获得schools-->
<school><!-- characters()->startElement()获得school -->
<key>1</key><!--characters()->startElement()获得key->characters()获得1->endElement()获得key-->
<value>河南大学</value><!--characters()->startElement()获得value->characters()获得”河南大学“->endElement()获得value-->
</school><!-- characters()->endElement()获得school -->
<school>
<key>2</key>
<value>河南大学软件学院</value>
</school>
<school>
<key>3</key>
<value>河南大学计算机学院</value>
</school>
</schools><!-- characters()->endElement()获得schools->endDocument() -->