XML Test Demo

  • A6_905724
    了解作者
  • 1.9MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-11 13:54
    上传日期
SAX Pull DOM 解析XML数据,以及http获取后台数据进行解析
XMl Test DEMO.zip
内容介绍
package com.btzh.myxmltest; import android.app.ProgressDialog; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; import org.w3c.dom.Attr; import org.w3c.dom.Comment; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView textView; private String httpStr = "http://10.27.7.162:8080/frame/lemeeting/getinforoom"; private String httpStrtest = "https://mail.qq.com/cgi-bin/readmail?sid=344d6HuF5sEcSiJ6&mailid=ZC3331-E0L~tCfZQmW4DWU9gT9aT73&nocheckframe=true&t=attachpreviewer&select=1&selectfile=&seq="; ProgressDialog progressDialog; String Strhttp = "\n" + "<lemeeting> <user>user9</user> " + "<conf_room> <conf_id>329</conf_id> <conf_name>?????</conf_name> " + " <start_time>2017-03-31 09:48:12</start_time> " + " <end_time>2017-04-01 09:48:12</end_time> " + "<conf_password>202cb962ac59075b964b07152d234b70</conf_password> " + " <group_id>1</group_id> " + " <world_id>600122</world_id> </conf_room></lemeeting>"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.spyq_text); findViewById(R.id.spyq_list_btn).setOnClickListener(this); findViewById(R.id.spyq_DOM_btn).setOnClickListener(this); findViewById(R.id.spyq_SAX_btn).setOnClickListener(this); findViewById(R.id.spyq_Pull_btn).setOnClickListener(this); progressDialog = new ProgressDialog(this); } private void showProgressDialog(){ if (null!=progressDialog){ progressDialog.show(); } } private void DismissProgressDialog(){ if (null!=progressDialog){ progressDialog.dismiss(); } } //开启异步线程 class Get_Result extends AsyncTask<String,Integer,String> { @Override protected void onPreExecute() { super.onPreExecute(); /* 设置进度条风格,风格为圆形,旋转的 */ progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); /* 设置ProgressDialog 标题 */ progressDialog.setTitle("提示"); /* 设置ProgressDialog 提示信息 */ progressDialog.setMessage("请稍后..."); /* 设置ProgressDialog 的进度条是否不明确 */ progressDialog.setIndeterminate(false); /* 设置ProgressDialog 是否可以按退回按键取消 */ progressDialog.setCancelable(true); showProgressDialog(); } @Override protected String doInBackground(String... params) { String getresult = http_get(httpStr); return getresult; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); DismissProgressDialog(); //对异步获取的到Xml格式的数据进行解析 saxParse(s); } } private void saxXmlParse(String XmlStr) { InputStream inputStream = null; try { inputStream = getResources().getAssets().open(XmlStr); } catch (IOException e) { e.printStackTrace(); } if(inputStream == null) return; final StringBuffer stringBuffer = new StringBuffer(); SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser saxParser = factory.newSAXParser(); DefaultHandler defaultHandler = new DefaultHandler(){ //开始解析文档,即开始解析XML根元素时调用该方法 @Override public void startDocument() throws SAXException { Log.d("zyr","startDocument"); super.startDocument(); } //开始解析每个元素时都会调用该方法 @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { Log.e("zyr","startElement " + qName); super.startElement(uri, localName, qName, attributes); stringBuffer.append("<" + qName); if(attributes!=null){ for(int i=0;i<attributes.getLength();i++){ stringBuffer.append(" " + attributes.getQName(i) + "=" + attributes.getValue(i)); } } stringBuffer.append(" rel='nofollow' onclick='return false;'>"); } //解析到每个元素的内容时会调用此方法 @Override public void characters(char[] ch, int start, int length) throws SAXException { Log.d("zyr","characters " + new String(ch,start,start+length)); super.characters(ch, start, length); stringBuffer.append(new String(ch,start,start+length)); } //每个元素结束的时候都会调用该方法 @Override public void endElement(String uri, String localName, String qName) throws SAXException { Log.e("zyr","endElement " + qName); super.endElement(uri, localName, qName); stringBuffer.append("</" + qName + ">"); } //结束解析文档,即解析根元素结束标签时调用该方法 @Override public void endDocument() throws SAXException { Log.d("zyr","endDocument"); super.endDocument(); runOnUiThread(new Runnable() { @Override public void run() { textView.setText(""); textView.setText(stringBuffer.toString()); } }); } }; saxParser.parse(inputStream,defaultHandler); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /*********************************SAX解析xml——start**********************************************/ private String get_qName = ""; private String get_value = ""; String value = ""; private void saxParse(String xmlStr) { if (xmlStr==null||"".equals(xmlStr)) return; fin
评论
    相关推荐