android 解析xml字串

來源:互聯網
上載者:User
package a.test;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.StringReader;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.InputSource;import org.xml.sax.XMLReader;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import biz.source_code.base64Coder.Base64Coder;import a.test.ExampleHandler;import a.test.ParsedExampleDataSet;import android.app.Activity;import android.os.Bundle;import android.util.Xml;import android.widget.EditText;import android.widget.TextView;public class b extends Activity {    /** Called when the activity is first created. */ EditText tv1; @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  tv1 = (EditText) findViewById(R.id.tv);  tv1.setText("000");  String xml = "<VCOM version='1.1'><loginlink>11111023</loginlink><errmsg>dfft</errmsg></VCOM>";  tv1.setText(xml);  ByteArrayInputStream tInputStringStream = null;  try  {  if (xml != null && !xml.trim().equals("")) {   tInputStringStream = new ByteArrayInputStream(xml.getBytes());  }  }  catch (Exception e) {   // TODO: handle exception   tv1.setText(e.getMessage());   return;  }  XmlPullParser parser = Xml.newPullParser();  try {   parser.setInput(tInputStringStream, "UTF-8");   int eventType = parser.getEventType();   while (eventType != XmlPullParser.END_DOCUMENT) {    switch (eventType) {    case XmlPullParser.START_DOCUMENT:// 文檔開始事件,可以進行資料初始化處理    // persons = new ArrayList<Person>();     break;    case XmlPullParser.START_TAG:// 開始元素事件     String name = parser.getName();     if (name.equalsIgnoreCase("errmsg")) {      // currentPerson = new Person();      // currentPerson.setId(new      // Integer(parser.getAttributeValue(null, "id")));      tv1.setText(parser.nextText());     } //      else if (currentPerson != null) {//      if (name.equalsIgnoreCase("loginlink")) {//       currentPerson.setName(parser.nextText());// 如果後面是Text節點,即返回它的值//      } else if (name.equalsIgnoreCase("errmsg")) {//       currentPerson.setAge(new Short(parser.nextText()));//      }//     }     break;    case XmlPullParser.END_TAG:// 結束元素事件//     if (parser.getName().equalsIgnoreCase("person")//       && currentPerson != null) {//      persons.add(currentPerson);//      currentPerson = null;//     }     break;    }    eventType = parser.next();   }   tInputStringStream.close();   // return persons;  } catch (XmlPullParserException e) {   // TODO Auto-generated catch block   e.printStackTrace();  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  } }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.