在android我们一般用dom或者sax来解析xml文件。
解析过程:
1.创建时间处理程序
2.创建sax解析器
3.将事件处理程序分配给解析器
4.对文档进行解析,将每个时间发送给解析器
由于android4.0以上做了较大幅度的改动,在程序中需要注意一下两个方面。
1.程序中是从网络中获取xml(我用tomcat搭建的服务器)所以需要有网络下载的程序。但是下载的程序不能再程序主线程中出现,具体的可以参考:http://blog.csdn.net/yuexin2/article/details/18868479
2.在以前的有些android的版本中在执行完endElement的时候不会再执行characters方法,但不知从什么时候开始,是需要执行的了。
在布局文件中很简单,就是一个button按钮,点击这个按钮会从网络下载xml文件,病进行解析。
=======ReadXMLActivity.java=============
package com.yx.readXML;
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.yx.utils.HttpDownLoad;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ReadXMLActivity extends Activity {
private Button readXML;
private HandlerThread thread;
private String readXMLtxt="";
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_xml);
readXML = (Button) findViewById(R.id.readXML);
readXML.setOnClickListener(new ReadXMLListenner());
thread = new HandlerThread("myGetXML_thread");
thread.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.read_xml, menu);
return true;
}
class ReadXMLListenner implements OnClickListener{
@Override
public void onClick(View arg0) {
handler = new Handler(thread.getLooper());
handler.post(run);
}
}
Runnable run = new Runnable() {
@Override
public void run() {
HttpDownLoad down = new HttpDownLoad();
readXMLtxt = down.downLoad("http://192.168.0.103:8080/testAndroidDownLoad/test.xml");//文件下载参考:http://blog.csdn.net/yuexin2/article/details/18868479
System.out.println("readXMLtxt--->"+readXMLtxt);
try {
//创建SAXParserFactory
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLReader reader = factory.newSAXParser().getXMLReader();
//为XMLReader设置内容处理器
reader.setContentHandler(new MyContentHandler());
//开始解析文件
reader.parse(new InputSource(new StringReader(readXMLtxt)));
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
==========MyContentHandler.java================
package com.yx.readXML;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyContentHandler extends DefaultHandler {
String eleName;
String hisName,sex,status,address,money;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println("进入characters--eleName is "+eleName);
if(eleName.equals("name")){
hisName = new String(ch,start,length);
System.out.println("hisName=="+hisName);
}
else if(eleName.equals("sex")){
sex = new String(ch,start,length);
}
else if(eleName.equals("status")){
status = new String(ch,start,length);
}
else if(eleName.equals("address")){
address = new String(ch,start,length);
}
else if(eleName.equals("money")){
money = new String(ch,start,length);
}
}
@Override
public void endDocument() throws SAXException {
//super.endDocument();
System.out.println("===========endDocument=============");
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(localName.equals("worker")){
this.printout();
}
eleName="";//将eleName清空,防止在执行endElement之后会将原来赋值的元素清空
}
@Override
public void startDocument() throws SAXException {
//super.startDocument();
System.out.println("===========startDocument=============");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//参数解释:uri正在解析文件的命名空间;localName:无前缀的文件名字;qName有前缀的文件名字;attributes标签的属性
//System.out.println("uri--->"+uri);
System.out.println("localName--->"+localName);
eleName = localName;
//System.out.println("qName--->"+qName);
System.out.println("attributes:");
if(localName.equals("worker")){
for (int i = 0; i < attributes.getLength(); i++) {
System.out.println(attributes.getLocalName(i)+"="+attributes.getValue(i));
}
}
}
public void printout(){
System.out.println("eleName-->"+eleName);
System.out.println("hisName-->"+hisName);
System.out.println("sex-->"+sex);
System.out.println("status-->"+status);
System.out.println("address-->"+address);
System.out.println("money-->"+money);
}
}
程序运行的顺序是:
startDocument->
之后,每次遇到一个标签就执行startElement->characters->endElement->characters
指导所有标签都完成后->endDocument
最后注意:由于需要访问网络,所以需要添加访问网络的权限。