android通过服务器后台webservices返回xml数据(问卷),保存本地,并通过sax解析xml

【1】android通过webservice接收xml字符

//调用webservices获取xml问卷

SoapObject QuestionRe =(SoapObject) RemoteWebservice.CallNewQuestion("116307");if (!QuestionRe.getProperty("wbsGetQuestionInfoResult").toString().equals("error")){//下载最新问卷if (XMLUtil.writeToXml(Login.this, QuestionRe.getProperty("wbsGetQuestionInfoResult").toString())== true){//Toast.makeText(Login.this,"已经下载最新问卷",Toast.LENGTH_LONG).show();}}

【2】把xml字符接收并保存到本地文件

/** * 字符串文本保存为本地XML * @param str xml字符串 * @return * @author fubin.pan  */    public static boolean writeToXml(Context context,String str)    {        String fileName = "admin_question.xml";try {OutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);try{OutputStreamWriter osw=new OutputStreamWriter(fos);osw.write(str);osw.close();fos.close();return true;} catch (IOException e) {e.printStackTrace();return false;}}catch (FileNotFoundException e) {e.printStackTrace();return false;}    }

【3】通过sax解析本地xml文件

FileInputStream inStream=questionitemlist2.this.openFileInput("admin_question.xml");Xml.parse(new InputStreamReader(inStream), quesHandler);
public class QuestionHandler extends DefaultHandler {private List<QuestionItem> list;private QuestionItem item;private String tag = "";private StringBuffer buffer;@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {super.characters(ch, start, length);if(item!=null){String data = new String(ch,start,length);if(tag.equals("code")){item.setCode(data);}else if(tag.equals("name")){item.setName(data);}else if(tag.equals("displayName")){item.setDisplayName(data);}else if(tag.equals("pubDate")){item.setitemType(data);}else if(tag.equals("toEnd")){item.setToEnd(data);}else if(tag.equals("toNext")){item.setToNext(data);}}}@Overridepublic void endDocument() throws SAXException {super.endDocument();}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {super.endElement(uri, localName, qName);if(localName.equals("item")){list.add(item);item = null;buffer = null;}tag = "";}@Overridepublic void startDocument() throws SAXException {super.startDocument();list = new ArrayList<QuestionItem>();}@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {super.startElement(uri, localName, qName, attributes);if(localName.equals("item")){item = new QuestionItem();buffer = new StringBuffer();}tag = localName;}public List<QuestionItem> getData(){return list;}}

【4】展现xml文件的界面

android webservices sax 传输并解析xml_第1张图片

更多相关文章

  1. Android根据联系人姓名首字符顺序读取通讯录
  2. Android TextView的字符过滤功能
  3. android 字符串加解密算法
  4. Android 输入框限制字符输入数
  5. 【Android 应用开发】Android资源文件 - 使用资源存储字符串 颜
  6. Android通过AES128加密解密字符串
  7. 2011.07.11(3)——— android xliff字符串操作
  8. android EditText 如何限制只能输入指定的字符
  9. android studio 报编码 GBK 的不可映射字符问题

随机推荐

  1. HTC Kaiser 上如何安装运行Android
  2. Activity总体介绍
  3. Android Service解析
  4. android studio中断开SVN连接,并彻底清理
  5. Android中ArrayAdapter和BaseAdapter的区
  6. Android 调试机制
  7. java Android SDK安装与环境变量配置以及
  8. Android JNI使用笔记
  9. Android(安卓)ListView 获取网络上的数据
  10. Android手机app启动的时候第一个Activity