ContentHandler.java:负责对XML进行处理
/**
*
*/
package com.duoguo.content;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* @author shyboy([email protected])
*
*/
public class ContentHandler extends DefaultHandler {
private String tagName;
private String name;
private String sex;
private int age;
// 解析XML文档开始部分
@Override
public void startDocument() throws SAXException {
System.out.println("start document");
}
// 解析XML文件结束部分
@Override
public void endDocument() throws SAXException {
System.out.println("end document");
}
// 解析XML文件元素开始部分
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// System.out.println("start element");
tagName = localName;
if ("user".equals(localName)) {
for (int i = 0; i < attributes.getLength(); i++)// 遍历元素的属性
{
System.out.println(attributes.getLocalName(i) + "="
+ attributes.getValue(i));
}
}
}
// 解析XML文件元素结束部分
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// System.out.println("end element");
if ("user".equals(localName)) {
printout();
}
}
// 解析XML文件的元素内容
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// System.out.println("characters");
if ("name".equals(tagName))// 当解析name元素的内容时
{
name = new String(ch, start, length);// 获取name元素的内容
} else if ("sex".equals(tagName))// 当解析sex元素的内容时
{
sex = new String(ch, start, length);// 获取sex元素的内容
} else if ("age".equals(tagName))// 当解析age元素的内容时
{
age = new Integer(new String(ch, start, length));// 获取age元素的内容
}
}
// 打印解析的XML文件的内容
public void printout() {
System.out.println("name:" + name);
System.out.println("sex:" + sex);
System.out.println("age:" + age);
}
}
XmlActivity.java
package com.duoguo.android;
import java.io.IOException;
import java.io.StringReader;
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;
import com.duoguo.content.ContentHandler;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 解析XML文件
*
* @author shyboy([email protected])
*
*/
public class XmlActivity extends Activity {
// 声明Button控件
private Button parseXMLButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
parseXMLButton = (Button) findViewById(R.id.parseXMLId);// 根据空间的ID获取Button空间
parseXMLButton.setOnClickListener(new ParseXMLButtonClick());// 为Button控件添加单击事件监听器
}
// 解析XML文件事件监听器
class ParseXMLButtonClick implements OnClickListener {
@Override
public void onClick(View v) {
System.out.println("click");
String xml = "<users><user id='001'><name>shyboy</name><sex>male</sex><age>24</age></user><user id='002'><name>playboy</name><sex>female</sex><age>20</age></user></users>";
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();// 实例化SAXParserFactory对象
try {
XMLReader xmlReader = saxParserFactory.newSAXParser()
.getXMLReader();// 获取XMLReader对象
xmlReader.setContentHandler(new ContentHandler());// 注册内容事件处理器
xmlReader.parse(new InputSource(new StringReader(xml)));// 开始解析XML
} catch (SAXException e) {
e.printStackTrace();
Log.d("debug", e.getMessage());
} catch (ParserConfigurationException e) {
e.printStackTrace();
Log.d("debug", e.getMessage());
} catch (IOException e) {
e.printStackTrace();
Log.d("debug", e.getMessage());
}
}
}
}