博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android基础之XML的创建与解析
阅读量:6070 次
发布时间:2019-06-20

本文共 4026 字,大约阅读时间需要 13 分钟。

hot3.png

写一个很简单的例子记录一下XML的创建与解析。

创建与解析其实是两个互逆的过程,会了其中一个对应的就能写出来。

创建用XmlSerializer xmlSerializer=Xml.newSerializer();

解析用XmlPullParser xmlPullParser=Xml.newPullParser();

package com.example.xmlborn;import java.io.IOException;import java.io.StringReader;import java.io.StringWriter;import java.util.ArrayList;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import org.xmlpull.v1.XmlSerializer;import android.R.xml;import android.os.Bundle;import android.app.Activity;import android.util.Xml;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener{	Button xmlpullButton;	private String result;	private TextView textView;	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		String resource=produceXML();		textView=(TextView) findViewById(R.id.text);		xmlpullButton=(Button) findViewById(R.id.button1);		xmlpullButton.setOnClickListener(this);	}	@Override	public void onClick(View v) {		// TODO Auto-generated method stub		switch (v.getId()) {		case R.id.button1:			result=parseXML();			if (result!=null) {				textView.setText(result);			}else {				Toast.makeText(getApplicationContext(),"null",1000).show();			}						break;		default:			break;		}	}	private String produceXML() {		// TODO Auto-generated method stub		StringWriter writer=new StringWriter();		XmlSerializer xmlSerializer=Xml.newSerializer();		ArrayList
list=getData(); try { xmlSerializer.setOutput(writer); xmlSerializer.startDocument("UTF-8",true); xmlSerializer.startTag(null,"beauties"); for (Beauty beauty:list) { xmlSerializer.startTag(null,"beautyname"); xmlSerializer.text(beauty.getNameString()); xmlSerializer.endTag(null,"beautyname"); xmlSerializer.startTag(null,"age"); xmlSerializer.text(beauty.getAge()); xmlSerializer.endTag(null,"age"); } xmlSerializer.endTag(null,"beauties"); xmlSerializer.endDocument(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return writer.toString(); } private ArrayList
getData() { // TODO Auto-generated method stub ArrayList
list=new ArrayList
(); Beauty lily=new Beauty("lily","23"); Beauty lucy=new Beauty("lucy","36"); list.add(lily); list.add(lucy); return list; } private String parseXML() { result="通过pull解析结果为:"; String xmlString=produceXML(); StringReader in=new StringReader(xmlString); XmlPullParser xmlPullParser=Xml.newPullParser(); try { xmlPullParser.setInput(in); int type=xmlPullParser.getEventType(); while (type!=XmlPullParser.END_DOCUMENT) { String nodeName=xmlPullParser.getName(); switch (type) { case XmlPullParser.START_TAG: if("beautyname".equals(nodeName)){ result +="姓名:"+xmlPullParser.nextText(); }if ("age".equals(nodeName)) { result +="年龄:"+xmlPullParser.nextText(); } break; } type=xmlPullParser.next(); } } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; // TODO Auto-generated method stub } class Beauty{ String nameString,age; public String getNameString() { return nameString; } public void setNameString(String nameString) { this.nameString = nameString; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Beauty(String nameString, String age) { super(); this.nameString = nameString; this.age = age; } }

转载于:https://my.oschina.net/u/1015229/blog/136107

你可能感兴趣的文章
C语言for循环的一些注意细节
查看>>
各种图形状图
查看>>
Sophos XG Firewall 通过同步式应用程序控制 突破网络可视度局限
查看>>
Xen缓冲区溢出漏洞(刘令)
查看>>
linux基础命令
查看>>
Apache如何开启Rewrite功能
查看>>
javah 详解
查看>>
Windows 系统常见操作
查看>>
走马观花: Linux 系统调用 open 七日游(一)
查看>>
请问怎样把 Real Player11录制的ivr格式音频转化为其他格式呢?
查看>>
crontab计划任务无法执行(小结)
查看>>
puppet三种认证注册方式详解及常见报错分析
查看>>
Field 'ssl_cipher' doesn't have a default value
查看>>
HDFS集中式缓存管理
查看>>
36.moquette源代码编译和运行
查看>>
Packet Tracer实验
查看>>
Java笔记9:构造方法,内部类
查看>>
PG PLProxy配置说明
查看>>
LVS 三种模式区别
查看>>
linux下一键编译安装MariaDB10.0.12
查看>>