易辰Android 2011-10-17
Android读取xml文件:
1.SAX
1.生成xml读取器SAXParser
2.创建自己的内容处理器,处理xml节点、属性等等,需要继承于 DefaultHandler 并且has a自己的数据结构
3.读取器1加载处理器2,读取xml
/* 从SAXParserFactory获取SAXParser. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* 从SAXParser获取XMLReader. */
XMLReader xr = sp.getXMLReader();
/* 创建我们自己的内容处理器*/
ExampleHandler myExampleHandler = new ExampleHandler();
/* 用内容处理器处理XMLReader*/
xr.setContentHandler(myExampleHandler);
/* XMLReader获取XML文件 */
xr.parse(new InputSource(url.openStream()));
/* 用我们的ExampleHandler解析XML中的数据. */
ParsedExampleDataSet parsedExampleDataSet =
myExampleHandler.getParsedData();
自己的处理器的数据结构:
private ParsedExampleDataSet myParsedExampleDataSet =
new ParsedExampleDataSet();
public ParsedExampleDataSet getParsedData() {
return this.myParsedExampleDataSet;
}
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new ParsedExampleDataSet();
Log.v("ParsingXML", "startDocument()");
}
@Override
public void endDocument() throws SAXException {
Log.v("ParsingXML", "endDocument()");
}
/** 当遇到开始标签时被调用,比如:
* <tag>
* 可以得到标签属性值,比如:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("outertag")) {
this.in_outertag = true;
}else if (localName.equals("innertag")) {
this.in_innertag = true;
}else if (localName.equals("mytag")) {
this.in_mytag = true;
}else if (localName.equals("tagwithnumber")) {
// 提取标签属性值
String attrValue = atts.getValue("thenumber");
int i = Integer.parseInt(attrValue);
myParsedExampleDataSet.setExtractedInt(i);
}
Log.v("ParsingXML", "StartElement(): "+ localName);
}
/** 当遇到终止标签时被调用,比如:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("outertag")) {
this.in_outertag = false;
}else if (localName.equals("innertag")) {
this.in_innertag = false;
}else if (localName.equals("mytag")) {
this.in_mytag = false;
}else if (localName.equals("tagwithnumber")) {
// Nothing to do here
}
Log.v("ParsingXML", "endElement(): "+ localName);
}
/** 当遇到如下结构时被调用
* <tag>characters</tag>
*/
@Override
public void characters(char ch[], int start, int length) {
if(this.in_mytag){
String str = new String(ch, start, length);
myParsedExampleDataSet.setExtractedString(str);
Log.v("ParsingXML", "characters(): " + str);
}
}
2.DOM
1.获取xml
2.构造DOM
3.获取xml内容
/*获取xml*/
String feed = getString(R.string.feed);
url = new URL(feed);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
/*构造DOM*/
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbfactory.newDocumentBuilder();
// 解析地震feed
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
// 清空旧的地震信息
infoList.clear();
/*获取xml内容*/
// 获得地震信息的列表
NodeList nl = docEle.getElementsByTagName("entry");
if (nl != null && nl.getLength() > 0) {
for (int i = 0 ; i < nl.getLength(); i++) {
Element entry = (Element)nl.item(i);
Element title = (Element)entry.getElementsByTagName("title").item(0);
Element geo = (Element)entry.getElementsByTagName("georss:point").item(0);
Element when = (Element)entry.getElementsByTagName("updated").item(0);
String details = title.getFirstChild().getNodeValue();
String point = geo.getFirstChild().getNodeValue();
String date = when.getFirstChild().getNodeValue();
}