实例化一个factory,通过XMLReader来读取解析。运行点击按钮如下显示:
02-19 21:01:58.661 17390-17390/? D/SAXContentHandler: This is Teacher 02-19 21:01:58.661 17390-17390/? D/SAXContentHandler: name is:xiao hong 02-19 21:01:58.661 17390-17390/? D/SAXContentHandler: age is:25 02-19 21:01:58.661 17390-17390/? D/SAXContentHandler: sex is:woman 02-19 21:01:58.661 17390-17390/? D/SAXContentHandler: class is:english 02-19 21:01:58.661 17390-17390/? D/SAXContentHandler: This is Student 02-19 21:01:58.661 17390-17390/? D/SAXContentHandler: name is:xiao ming 02-19 21:01:58.661 17390-17390/? D/SAXContentHandler: age is:15 02-19 21:01:58.661 17390-17390/? D/SAXContentHandler: sex is:man可以发现已经得到我们想要的信息了。
接着使用Pull方式,开始解析可以通过调用它的next方法,获取下一个事件,可以通过getAttribute方法获取属性,通过nextText方法来获取节点的值。编写代码如下:
package com.jared.emxmlstudy; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; import java.io.StringReader; import javax.xml.parsers.SAXParserFactory; import cz.msebera.android.httpclient.Header; public class MainActivity extends AppCompatActivity { private static final String TAB = "XMLParse"; private static final String xmlUrl = ""; private Button mGetXmlSax; private Button mGetXmlPull; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGetXmlSax = (Button)findViewById(R.id.getXmlsax); mGetXmlPull = (Button)findViewById(R.id.getXmlpull); mGetXmlSax.setOnClickListener(new myOnClickListener()); mGetXmlPull.setOnClickListener(new myOnClickListener()); } private class myOnClickListener implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.getXmlsax: sendRequestWithSax(xmlUrl); break; case R.id.getXmlpull: sendRequestWithPull(xmlUrl); break; default: break; } } } private void sendRequestWithPull(String url) { AsyncHttpClient client = new AsyncHttpClient(); client.get(url, new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { try { String response = new String(bytes, 0, bytes.length, "utf-8"); parseXMLWithPull(response); } catch (Exception e) { e.printStackTrace(); } } @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { } }); } private void parseXMLWithPull(String xmlData) { try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); xmlPullParser.setInput(new StringReader(xmlData)); int eventType = xmlPullParser.getEventType(); String mName = ""; String mAge = ""; String mSex = ""; String mClass = ""; while (eventType != XmlPullParser.END_DOCUMENT) { String nodeName = xmlPullParser.getName(); switch (eventType) { case XmlPullParser.START_TAG: { if("name".equals(nodeName)) { mName = xmlPullParser.nextText(); } else if("age".equals(nodeName)) { mAge = xmlPullParser.nextText(); } else if("sex".equals(nodeName)) { mSex = xmlPullParser.nextText(); } else if("class".equals(nodeName)) { mClass = xmlPullParser.nextText(); } break; } case XmlPullParser.END_TAG: { if("Teacher".equals(nodeName)) { Log.d(TAB, "This is Teacher"); Log.d(TAB, "name is:" + mName.trim()); Log.d(TAB, "age is:" + mAge.trim()); Log.d(TAB, "sex is:" + mSex.trim()); Log.d(TAB, "class is:" + mClass.trim()); } else if("Student".equals(nodeName)) { Log.d(TAB, "This is Student"); Log.d(TAB, "name is:" + mName.trim()); Log.d(TAB, "age is:" + mAge.trim()); Log.d(TAB, "sex is:" + mSex.trim()); } } default: break; } eventType = xmlPullParser.next(); } } catch (Exception e) { e.printStackTrace(); } } private void sendRequestWithSax(String url) { AsyncHttpClient client = new AsyncHttpClient(); client.get(url, new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { try { String response = new String(bytes, 0, bytes.length, "utf-8"); parseXMLWithSax(response); } catch (Exception e) { e.printStackTrace(); } } @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { } }); } private void parseXMLWithSax(String xmlData) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); XMLReader xmlReader = factory.newSAXParser().getXMLReader(); SAXContentHandler mHandler = new SAXContentHandler(); xmlReader.setContentHandler(mHandler); xmlReader.parse(new InputSource(new StringReader(xmlData))); } catch (Exception e) { e.printStackTrace(); } } }运行结果如下: 02-19 21:52:01.391 19388-19388/? D/XMLParse: This is Teacher 02-19 21:52:01.391 19388-19388/? D/XMLParse: name is:xiao hong 02-19 21:52:01.391 19388-19388/? D/XMLParse: age is:25 02-19 21:52:01.391 19388-19388/? D/XMLParse: sex is:woman 02-19 21:52:01.391 19388-19388/? D/XMLParse: class is:english 02-19 21:52:01.391 19388-19388/? D/XMLParse: This is Student 02-19 21:52:01.391 19388-19388/? D/XMLParse: name is:xiao ming 02-19 21:52:01.391 19388-19388/? D/XMLParse: age is:15 02-19 21:52:01.391 19388-19388/? D/XMLParse: sex is:man
最后一种是Dom方式,Dom方式主要比较耗费内存,需要遍历所有,一般手机上的app开发不太适用。那就简单实现下吧,还是利用Async-HttpClient,接着编写代码如下: private void parseXMLWithDom(String xmlData) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); String mName = ""; String mAge = ""; String mSex = ""; String mClass = ""; try { DocumentBuilder builder = factory.newDocumentBuilder(); //Document document = builder.parse(xmlData); Document document = builder.parse(new InputSource(new StringReader(xmlData))); Element root = document.getDocumentElement(); Log.d(TAB, "根节点名称:" + root.getTagName()); NodeList items = root.getElementsByTagName("Teacher"); Element personElement = (Element)items.item(0); Log.d(TAB, "根节点名称:" + personElement.getTagName()); NodeList childNodes = personElement.getChildNodes(); Log.d(TAB, "This is Teacher"); for(int i = 0; i < childNodes.getLength(); i++) { Node grandElement = childNodes.item(i); if(grandElement.getNodeType() == Node.ELEMENT_NODE) { if("name".equals(grandElement.getNodeName())) { mName = grandElement.getFirstChild().getNodeValue(); Log.d(TAB, "name is:" + mName.trim()); } else if("age".equals(grandElement.getNodeName())) { mAge = grandElement.getFirstChild().getNodeValue(); Log.d(TAB, "age is:" + mAge.trim()); } else if("sex".equals(grandElement.getNodeName())) { mSex = grandElement.getFirstChild().getNodeValue(); Log.d(TAB, "sex is:" + mSex.trim()); } else if("class".equals(grandElement.getNodeName())) { mClass = grandElement.getFirstChild().getNodeValue(); Log.d(TAB, "class is:" + mClass.trim()); } } } } catch (Exception e){ e.printStackTrace(); } }
这里简单的就实现了Teacher的,student的就没有添加,相信也是很容易的了,其余的代码和上述类似,运行效果如下: 02-20 09:16:49.601 1353-1353/? D/XMLParse: 根节点名称:Person 02-20 09:16:49.601 1353-1353/? D/XMLParse: 根节点名称:Teacher 02-20 09:16:49.601 1353-1353/? D/XMLParse: This is Teacher:9 02-20 09:16:49.601 1353-1353/? D/XMLParse: name is:xiao hong 02-20 09:16:49.601 1353-1353/? D/XMLParse: age is:25 02-20 09:16:49.601 1353-1353/? D/XMLParse: sex is:woman 02-20 09:16:49.601 1353-1353/? D/XMLParse: class is:english
关于xml基本上先学习这些知识了。接着学习json的知识。
首先和xml一样,新建一个person.json文件,如下:
1 [{"name":"xiao hong", "age":"25", "sex":"wonan"},
2 {"name":"xiao ming", "age":"15", "sex":"man"},
3 {"name":"xiao qiang", "age": 30, "sex":"man"}]
保存到和xml同一级目录下,运行浏览器如下图所示: