android webservices sax 传输并解析xml
2011-04-14 17:30
399 查看
android通过服务器后台webservices返回xml数据(问卷),保存本地,并通过sax解析xml
【1】android通过webservice接收xml字符
//调用webservices获取xml问卷
【2】把xml字符接收并保存到本地文件
【3】通过sax解析本地xml文件
【4】展现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; @Override public 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); } } } @Override public void endDocument() throws SAXException { super.endDocument(); } @Override public 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 = ""; } @Override public void startDocument() throws SAXException { super.startDocument(); list = new ArrayList<QuestionItem>(); } @Override public 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解析XML总结(SAX、Pull、Dom三种方式)附带DOM4J、JDOM
- Android中解析XML文件并传输数据
- android之sax解析xml文件
- android解析XML总结(SAX、Pull、Dom三种体式格式)
- android xml解析之SAX
- 一个android中使用sax解析xml的例子
- Android系列--DOM、SAX、Pull解析XML
- android xml-----Sax解析
- 我的Android进阶之旅------>Android中解析XML 技术详解---->SAX解析、DOM解析、PULL解析
- Android平台中实现对XML的三种解析方式(DOM 、PULL、 SAX)
- Android开发:XML简介及DOM、SAX、PULL解析对比
- Android中解析XML_DOM_SAX_PULL
- android sax解析 xml方式
- Android开发:XML简介及DOM、SAX、PULL解析对比
- Android网络之数据解析----SAX方式解析XML数据
- Android解析Xml的三种方式:DOM、SAX以及XMLPull
- android xml解析技术 json数据传输技术
- Android解析和创建XML之学习笔记------SAX
- Android 中利用SAX ,Dom,pull解析 XML 文件
- android sax dom pull 解析xml 和生成xml