安卓学习体会2:XML文本的程序生成
生成XML文本
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="1">
<name>john</name>
<age>30</age>
</person>
</persons>
import java.io.*;
import org.xmlpull.v1.*;
import android.util.Xml;
button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try
{
FileOutputStream oS=new FileOutputStream("/sdcard/test.xml");
XmlSerializer xml=Xml.newSerializer();
xml.setOutput(oS, "UTF-8");
xml.startDocument("UTF-8", true);
xml.startTag(null, "persons");
xml.startTag(null, "person");
xml.attribute(null , "id", "1");
xml.startTag(null, "name");
xml.text("john");
xml.endTag(null, "name");
xml.startTag(null,"age");
xml.text("30");
xml.endTag(null, "age");
xml.endTag(null,"person");
xml.endTag(null, "persons");
xml.endDocument();
oS.flush();
oS.close();
}
catch(Exception e)
{
setTitle(e.toString());
}
}
}); //new OnClickListener
}