1.首先/获取xml文件,在assert文件夹目录下面!
private void parseXml() {
InputStream setxmlStream = null;
try {
setxmlStream = NgnApplication.getContext().getAssets().open(
"set.xml"); // // 從assets中讀入數據源
MySAXParser parser = new MySAXParser();
parser.parse(setxmlStream); // 對輸入流進行解析
result = parser.getResult(); // 獲得解析結果
} catch (ParserConfigurationException e) {
Log.e(TAG, e.getMessage());
} catch (SAXException e) {
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
if (setxmlStream != null) {
try {
setxmlStream.close();
} catch (IOException e) {
}
setxmlStream = null;
}
}
///自定义一个saxParser
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
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;
public class MySAXParser {
public static final String ELEMENT_STRING = "GetSettingString";
public static final String ELEMENT_INT = "GetSettingInt";
public static final String ELEMENT_SET = "set";
private List<SetXml> result = new LinkedList<SetXml>();
private SetXml setxml;
private boolean isSet, intNode, stringNode;
public void parse(InputStream xml) throws ParserConfigurationException,
SAXException, IOException {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); // 調用靜態方法newInstance得到SAXParseFactory實例
SAXParser saxParser = saxParserFactory.newSAXParser(); // 調用newSAXParser創建SAXParser對象
XMLReader xmlReader = saxParser.getXMLReader(); // 獲得XMLReader對象
xmlReader.setContentHandler(new MySAXHandler()); // 設置處理XML的Handler
xmlReader.parse(new InputSource(xml)); // 將InputStream裝飾為InputSource,進行解析
}
private class MySAXHandler extends DefaultHandler {
// private String currentElement;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
String tagName = localName.length() != 0 ? localName : qName;
// tagName = tagName.toLowerCase().trim();
// 如果读取的是river标签开始,则实例化River
if (tagName.equals(ELEMENT_SET)) {
isSet = true;
setxml = new SetXml();
} // 然后读取其他节点
if (isSet) {
if (tagName.equals(ELEMENT_INT)) {
intNode = true;
} else if (tagName.equals(ELEMENT_STRING)) {
stringNode = true;
}
}
}
public void characters(char[] ch, int start, int length) {
// 设置属性值
if (intNode) {
setxml.setSetKey(ELEMENT_INT);
setxml.setSetValue(new String(ch, start, length));
} else if (stringNode) { // 解决null问题
setxml.setSetKey(ELEMENT_STRING);
setxml.setSetValue(new String(ch, start, length));
}
}
public void endElement(String uri, String localName, String qName) {
String tagName = localName.length() != 0 ? localName : qName;
// tagName = tagName.toLowerCase().trim();
// 如果读取的是river标签结束,则把River添加进集合中
if (tagName.equals(ELEMENT_SET)) {
isSet = true;
result.add(setxml);
} // 然后读取其他节点
if (isSet) {
if (tagName.equals(ELEMENT_INT)) {
intNode = false;
} else if (tagName.equals(ELEMENT_STRING)) {
stringNode = false;
}
}
}
}
3.set。xml的节点对象
public List<SetXml> getResult() {
return this.result;
}
class SetXml {
public SetXml() {
}
private String setKey;
private String setValue;
public String getSetKey() {
return setKey;
}
public void setSetKey(String setKey) {
this.setKey = setKey;
}
public String getSetValue() {
return setValue;
}
public void setSetValue(String setValue) {
this.setValue = setValue;
}
}
}
---------------------------4 set.xml ----------------------
<?xml version="1.0" encoding="utf-8"?>
<Sets>
<set>
<GetSettingString>GetSettingString_VPX_888</GetSettingString>
</set>
<set>
<GetSettingInt>320</GetSettingInt>
</set>
</Sets>