private void xmlPull(String xmlStr){
XmlPullParserFactory factory= null;
try {
factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xmlStr));
int eventType = xmlPullParser.getEventType();
String id="";
String name="";
String version="";
while (eventType!=XmlPullParser.END_DOCUMENT){
String nodeName=xmlPullParser.getName();
switch (eventType){
case START_TAG:{
if("id".equals(nodeName)){
id=xmlPullParser.nextText();
}else if("name".equals(nodeName)){
name=xmlPullParser.nextText();
}else if("version".equals(nodeName)){
version=xmlPullParser.nextText();
}
break;
}
case END_TAG:{
if("app".equals(nodeName)){
Log.d(TAG, "xmlPull: id is:"+ id);
Log.d(TAG, "xmlPull: name is:"+ name);
Log.d(TAG, "xmlPull: version is:" + version);
}
break;
}
default:
break;
}
try {
eventType = xmlPullParser.next();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
------------------------------------------------------------------------------------
private void xmlSax(String xmlStr){
try {
SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
XMLReader xmlReader=saxParserFactory.newSAXParser().getXMLReader();
SaxHandler saxHandler=new SaxHandler();
xmlReader.setContentHandler(saxHandler);
xmlReader.parse(new InputSource(new StringReader(xmlStr)));
Log.d(TAG, "xmlSax: 调用了程序了");
} catch (Exception e) {
e.printStackTrace();
}
}
//新建类,这个是xmlSax解析的
package com.example.httpactivity;
import android.util.Log;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxHandler extends DefaultHandler {
private String nodeName;
private StringBuilder id;
private StringBuilder name;
private StringBuilder version;
@Override
public void startDocument() throws SAXException {
super.startDocument();
id = new StringBuilder();
name=new StringBuilder();
version=new StringBuilder();
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
nodeName=localName;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
if("app".equals(localName)){
Log.d("SaxHandler", "id is:"+id);
Log.d("SaxHandler", "name is:"+name);
Log.d("SaxHandler", "version is:"+version);
//最后要将StringBuild清空
id.setLength(0);
name.setLength(0);
version.setLength(0);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
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);
}
}
}
---------------------------------------------------------------
private void jsonParse(String jsStr){
try {
JSONArray jsonArray=new JSONArray(jsStr);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
String id=jsonObject.getString("id");
String version=jsonObject.getString("version");
//String name=jsonObject.getString("name");
Log.d(TAG, "jsonParse id: "+id);
Log.d(TAG, "jsonParse version: "+version);
//Log.d(TAG, "jsonParse name: "+name);;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Android XML JSON解析
最新推荐文章于 2023-07-10 08:15:00 发布