写一个很简单的例子记录一下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<Beauty> 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<Beauty> getData() {
- // TODO Auto-generated method stub
- ArrayList<Beauty> list=new ArrayList<Beauty>();
- 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;
- }
- }
- }
- 代码地址: