Android Parse XML format string

Source: Internet
Author: User
Tags string format

The recent lab's Android app requires the ability to send internal mail.

White is simply a database of additions and deletions processing.

But one of the interesting parts of the middle is getting the list of recipients to handle.

When the user logs in to the app, if the authentication succeeds, the server sends all the information of the data contact back to the client.

The format that is sent out is a dataset, which is processed after the string that is converted to XML format is sent out.

When sending an e-mail message, a spinner control is provided to the recipient, and the name of everyone is displayed.

This time it involves parsing the string in XML format.

My approach to implementation is simple. The Pull method is used. Hope to be able to help the students in need.

First look at the string format of the server-side send back

<span style= "FONT-SIZE:18PX;" ><NewDataSet><ds><UserName>admin</UserName></ds><ds><UserName> Wang Han Yang </UserName></ds><ds><UserName> Chen Peng </UserName></ds><ds><UserName> Shei Yongjun </UserName></ds><ds><UserName> Minchen </username></ds><ds><username > Kangkuan </UserName></ds><ds><UserName> Liu Yuan </UserName></ds><ds>< username> fanfan </UserName></ds><ds><UserName> Zhao Jing </UserName></ds><ds> <UserName> Huang </UserName></ds><ds><UserName> Xu Lan </username></ds><ds ><UserName> Zhaojinzha </UserName></ds><ds><UserName> Ning Yunxia </UserName></ds> <ds><UserName> Jiahui </UserName></ds><ds><UserName> Wang Rui </username></ds ><ds><UserName> Timothy Project </UserName></ds><ds><UserName> Li Ting </username></ds><ds><UserName> Kang Jianqi </UserName></ds><ds><UserName> Folding super-ying </ username></ds><ds><username> Liuliang </UserName></ds><ds><UserName> Pang </UserName></ds><ds><UserName> Shang </UserName></ds><ds><UserName> Shang </UserName></ds><ds><UserName> Stone into </username></ds><ds><username > Zhang Zhenlong </UserName></ds><ds><UserName> Wang Yanjun </UserName></ds><ds>< username> bankbook ze yu </UserName></ds><ds><UserName> yangjie </UserName></ds><ds> <UserName> Dourdan </UserName></ds><ds><UserName> Chaoxiao </username></ds><ds ><UserName> Zhaohui </UserName></ds><ds><UserName> Lu Shingyu </UserName></ds> <ds><UserName> Tian Baofeng </UserName></ds><ds><UserName> Wang Ling </username></ds ><ds><username> Liu Yiqun </UserName></ds><ds><UserName> He Yan </UserName></ds><ds>< username> Wen Ye </UserName></ds><ds><UserName> Houli Branch </UserName></ds><ds> <UserName> Liu </UserName></ds><ds><UserName> Wang Qingwei </username></ds><ds ><UserName> Wang Xu </UserName></ds><ds><UserName> Hongchesong </UserName></ds> <ds><UserName> Zhang Xiaping </UserName></ds><ds><UserName> Li Ruixia </username></ds ><ds><UserName> Qian </UserName></ds></NewDataSet></span>

Is the name of everyone in the database.

The client then accepts the string. Also write to Sharedpreferences to save.

When sending an email, read the name of the sharedpreferences and convert it.

Read Name:

<span style= "FONT-SIZE:18PX;" >private sharedpreferences info;private String peoinfo;</span>
<span style= "FONT-SIZE:18PX;" >info = getsharedpreferences ("info", 0);p eoinfo = info.getstring ("info", "");</span>
<span style= "FONT-SIZE:18PX;" >bytearrayinputstream Tinputstringstream = new Bytearrayinputstream (Peoinfo.getbytes ()); <span style= " White-space:pre "></span>//to flow </span>
XML string processing, first declares an interface Infoparse.java:

<span style= "FONT-SIZE:18PX;" >public interface Infoparse {public arraylist<string> parse (InputStream is) throws Exception;} </span>
return to ArrayList;

Then implement the interface Pullinfo.java:

<span style= "FONT-SIZE:18PX;" >public class Pullinfo implements Infoparse {arraylist<string> info = new arraylist<string> (); String temp =null; @Overridepublic arraylist<string> Parse (InputStream is) throws Exception {//TODO Auto-generated method stub xmlpullparser parser = Xml.newpullparser ();          Create a Xmlpullparser instance Parser.setinput (IS, "UTF-8") by ANDROID.UTIL.XML;          int eventtype = Parser.geteventtype (); while (eventtype! = xmlpullparser.end_document) {switch (eventtype) {case Xmlpullparser.start_         Document:temp = null;         Break  Case XmlPullParser.START_TAG:if (Parser.getname (). Equals ("UserName")) {EventType                       = Parser.next ();                                      temp = Parser.gettext ();         } break;               Case XmlPullParser.END_TAG:if (Parser.getname (). Equals ("DS")) {Info.add (temp);       } break;                  } EventType = Parser.next (); }<span style= "White-space:pre" ></span>return info;<span style= "White-space:pre" &GT;&LT;/SPAN&GT;} </span>
<span style= "FONT-SIZE:18PX;" >}</span>
because there are strings to see, only the contents of a tag in the DS need to be identified. For other labels, add your own criteria.

Finally, write in the email

<span style= "White-space:pre" ><span style= "font-size:18px;" >infoparse parser = null;</span></span>
<pre name= "code" class= "java" ><span><span style= "font-size:18px;" >ArrayList<String> peoname = new arraylist<string> ();</span></span>
String drp[];
<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span>try {parser = new Pullinfo ();p eoname =  Parser.parse ( tinputstringstream); int size = Peoname.size ();d rp = new String[size];for (int i = 0; i < size; I + +) {Drp[i] = (String) p Eoname.get (i);}} catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();} </span>
After this parsing, there is a string array in the DRP.

As for bindings, you can refer to the following

Private Spinner type;private arrayadapter<string> adapter;type = (Spinner) Findviewbyid (r.id.type);        Type.setpadding (15,0,0,0);        adapter = new Arrayadapter<string> (this,android. R.LAYOUT.SIMPLE_SPINNER_ITEM,DRP);        Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);        Type.setadapter (adapter); Type.setonitemselectedlistener (New Onitemselectedlistener () {@Overridepublic void onitemselected (adapterview<? > arg0, View arg1,int arg2, long Arg3) {//TODO auto-generated method stub touser = Drp[arg2];} @Overridepublic void onnothingselected (adapterview<?> arg0) {//TODO auto-generated method stub}                           });
Effects such as.

Spit trough A sentence: self-study Android less than 1 months of the Junior dog, no art, only one person to do Android app,

O (︶^︶) O Alas ~ will see ~







Android Parse XML format string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.