android解析XML(PULL方法与SAX方法)
解析结果:
Internet权限记得打开
D/kwwl: response.code()==200
D/kwwl: response.message()==OK
D/kwwl: res == <apps>
<app>
<id>1</id>
<name>Google Maps</name>
<version>1.0</version>
</app>
<app>
<id>2</id>
<name>Chrome</name>
<version>2.1</version>
</app>
<app>
<id>3</id>
<name>Google Play</name>
<version>2.3</version>
</app>
</apps>
I/System.out: pull方法解析xml
D/MainActivity: id is 1
D/MainActivity: name is Google Maps
D/MainActivity: version is 1.0
D/MainActivity: id is 2
D/MainActivity: name is Chrome
D/MainActivity: version is 2.1
D/MainActivity: id is 3
D/MainActivity: name is Google Play
D/MainActivity: version is 2.3
I/System.out: sax方法解析xml
I/System.out: 开始解析的节点为localName==nodeName== apps
I/System.out: 开始解析的节点为localName==nodeName== app
I/System.out: 开始解析的节点为localName==nodeName== id
I/System.out: 解析完此节点为localName:id
I/System.out: 此时的nodeName为: id
I/System.out: 开始解析的节点为localName==nodeName== name
I/System.out: 解析完此节点为localName:name
I/System.out: 此时的nodeName为: name
I/System.out: 开始解析的节点为localName==nodeName== version
I/System.out: 解析完此节点为localName:version
I/System.out: 此时的nodeName为: version
I/System.out: 解析完此节点为localName:app
I/System.out: 此时的nodeName为: version
E/ContentHandler: id is 1
E/ContentHandler: name is Google Maps
E/ContentHandler: version is 1.0
I/System.out: 开始解析的节点为localName==nodeName== app
I/System.out: 开始解析的节点为localName==nodeName== id
I/System.out: 解析完此节点为localName:id
I/System.out: 此时的nodeName为: id
I/System.out: 开始解析的节点为localName==nodeName== name
I/System.out: 解析完此节点为localName:name
I/System.out: 此时的nodeName为: name
I/System.out: 开始解析的节点为localName==nodeName== version
I/System.out: 解析完此节点为localName:version
I/System.out: 此时的nodeName为: version
I/System.out: 解析完此节点为localName:app
I/System.out: 此时的nodeName为: version
E/ContentHandler: id is 2
E/ContentHandler: name is Chrome
E/ContentHandler: version is 2.1
I/System.out: 开始解析的节点为localName==nodeName== app
I/System.out: 开始解析的节点为localName==nodeName== id
I/System.out: 解析完此节点为localName:id
I/System.out: 此时的nodeName为: id
I/System.out: 开始解析的节点为localName==nodeName== name
I/System.out: 解析完此节点为localName:name
I/System.out: 此时的nodeName为: name
I/System.out: 开始解析的节点为localName==nodeName== version
I/System.out: 解析完此节点为localName:version
I/System.out: 此时的nodeName为: version
I/System.out: 解析完此节点为localName:app
I/System.out: 此时的nodeName为: version
E/ContentHandler: id is 3
E/ContentHandler: name is Google Play
E/ContentHandler: version is 2.3
I/System.out: 解析完此节点为localName:apps
I/System.out: 此时的nodeName为: version
OKHTTP实现代码
private void sendRequestWithOKHttp(){
new Thread(new Runnable() {
@Override
public void run() {
try {
String string_url = editText.getText().toString();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(string_url)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
showToastResponse("response.isSuccessful() == true 打开成功");
Log.d("kwwl", "response.code()==" + response.code());
Log.d("kwwl", "response.message()==" + response.message());
showToastResponse("response.cod e()==" + response.code());
showToastResponse("response.message()==" + response.message());
String responseData = response.body().string();
showResponse(responseData);
Log.d("kwwl","res == " + responseData);
System.out.println("pull方法解析xml");
parseXMLWithPull(responseData);
System.out.println("sax方法解析xml");
parseXMLWithSAX(responseData);
} else {
showToastResponse("response.isSuccessful() == false 打开失败");
showToastResponse("response.code()==" + response.code());
showToastResponse("response.message()==" + response.message());
}
}
catch (Exception e){
e.printStackTrace();
}
}
}).start();
}
private void showResponse(final String response){
runOnUiThread(new Runnable() {
/*安卓不允许在子线程中进行UI操作,
所以调用runOnUIThread将线程切换到主线程然后再更新UI元素*/
@Override
public void run() {
responseText.setText(response);
}
});
}
private void showToastResponse(final String ToastText){
//打印toast
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, ToastText, Toast.LENGTH_SHORT).show();
}
});
}
Apache服务器配置
在127.0.0.1放置一个XML文件get_data.xml(http://127.0.0.1/get_data.xml在android模拟器中地址为http://10.0.2.2/get_data_xml)
PULL方法实现代码
private void parseXMLWithPull(String xmlData){
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
/*获取一个XmlPullParserFactory实例,借助这个实例得到xmlPullParser对象*/
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xmlData));
/*调用setPut方法将xml的数据传入*/
int eventType = xmlPullParser.getEventType();
/*getEventType得到当前解析事件*/
String id = "";
String name = "";
String version = "";
while(eventType != XmlPullParser.END_DOCUMENT){
/*如果evenType不等于END_DOCUMENT则说明解析工作没完成,继续循环*/
String nodeName = xmlPullParser.getName();
/*得到当前节点的名字*/
switch(eventType){
//开始解析某个节点
case XmlPullParser.START_TAG:{
if("id".equals(nodeName)){
id = xmlPullParser.nextText();
/*nextText()获取当前节点中存放的具体内容*/
} else if("name".equals(nodeName)){
name = xmlPullParser.nextText();
} else if("version".equals(nodeName)){
version = xmlPullParser.nextText();
}
break;
}
//完成解析某个节点
case XmlPullParser.END_TAG:{
if("app".equals(nodeName)){
Log.d("MainActivity","id is " + id);
Log.d("MainActivity","name is " + name);
Log.d("MainActivity","version is " + version);
}
break;
}
default:break;
}
eventType = xmlPullParser.next();
/*调用next方法获取下一个解析事件*/
}
}catch(Exception e){
e.printStackTrace();
}
}
SAX 实现代码
SAX操作函数
private void parseXMLWithSAX(String xmlData){
try{
//创建一个factory对象
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
//获取XMLReader对象
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
//新建一个contentHandler实例
ContentHandler contentHandler = new ContentHandler();
//将contentHandler实例设置到xmlreader中
xmlReader.setContentHandler(contentHandler);
//开始执行解析
xmlReader.parse(new InputSource(new StringReader(xmlData)));
}catch(Exception e){
e.printStackTrace();
}
}
ContentHandler类
public class ContentHandler extends DefaultHandler {
private String nodeName;
private StringBuilder id;
private StringBuilder name;
private StringBuilder version;
@Override
public void startDocument() throws SAXException {
id = new StringBuilder();
name = new StringBuilder();
version = new StringBuilder();
//初始化三个builder
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//开始解析某个节点时,调用startElement()
//记录当前节点名称:localname存放的是当前节点的名称,这里把他取出来。
nodeName = localName;
System.out.println("开始解析的节点为localName==nodeName== " + nodeName);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
//解析具体节点内容时会调用characters()
//将解析出来的内容添加到builder中
if("id".equals(nodeName)){
id.append(ch,start,length);
} else if("name".equals(nodeName)){
name.append(ch,start,length);
} else if("version".equals(nodeName)){
version.append(ch,start,length);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
/*解析完节点内部内容之后调用endElement()*/
System.out.println("解析完此节点为localName:" + localName + "\n此时的nodeName为: " + nodeName);
if("app".equals(localName)){
//必须使用localname,不能用nodename
Log.e("ContentHandler","id is " + id.toString().trim());
/*trim()删除字符串头尾的空白符*/
Log.e("ContentHandler","name is " + name.toString().trim());
Log.e("ContentHandler","version is " + version.toString().trim());
/*清空三个builder的内容*/
id.setLength(0);
name.setLength(0);
version.setLength(0);
}
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
}