[b][size=large]1.读[/size][/b]
LinkMan.java
XmlPullUtil.java
Main.java
[b][size=large]2.写[/size][/b]
XmlPullUtil.java
Main.java
LinkMan.java
package com.example.xmlpulldemo;
public class LinkMan {
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
XmlPullUtil.java
package com.example.xmlpulldemo;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
public class XmlPullUtil {
private InputStream input;
private List<LinkMan> all=null;
private String curElement=null;
private LinkMan man=null;
public XmlPullUtil(InputStream input) {
this.input = input;
}
public List<LinkMan> getAll(){
XmlPullParserFactory factory=null;
XmlPullParser parser=null;
try {
factory=XmlPullParserFactory.newInstance();
parser=factory.newPullParser();
parser.setInput(input, "UTF-8");
int eventType=parser.getEventType();//事件码
while(eventType!=XmlPullParser.END_DOCUMENT){
switch(eventType){
case XmlPullParser.START_DOCUMENT:
all=new ArrayList<LinkMan>();
break;
case XmlPullParser.START_TAG:
curElement=parser.getName();
if(curElement.equals("linkman")){
man=new LinkMan();
}
break;
case XmlPullParser.TEXT:
if(curElement.equals("name")){
man.setName(parser.getText());
}else if(curElement.equals("email")){
man.setEmail(parser.getText());
}
break;
case XmlPullParser.END_TAG:
curElement=parser.getName();
if(curElement.equals("linkman")){
all.add(man);
man=null;
}
break;
case XmlPullParser.END_DOCUMENT:
break;
default:
break;
}
eventType=parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return all;
};
}
Main.java
package com.example.xmlpulldemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
private TextView name=null;
private TextView email=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name=(TextView)findViewById(R.id.name);
email=(TextView)findViewById(R.id.email);
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=new File(Environment.getExternalStorageDirectory()+File.separator+"files/addresslist.xml");
if(!file.exists()){//文件不存在
return;
}
InputStream input=null;
try {
input = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XmlPullUtil util=new XmlPullUtil(input);
List<LinkMan> all=util.getAll();
name.setText(all.get(0).getName());
email.setText(all.get(0).getEmail());
}else{
Toast.makeText(this, "没有sdcard", Toast.LENGTH_LONG).show();
}
}
}
[b][size=large]2.写[/size][/b]
XmlPullUtil.java
package com.example.xmlpulldemo;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
public class XmlPullUtil {
private OutputStream output;
private List<LinkMan> all;
public XmlPullUtil(OutputStream output,List<LinkMan> all) {
this.output=output;
this.all=all;
}
public void save(){
XmlPullParserFactory factory=null;
XmlSerializer xs=null;
try {
factory=XmlPullParserFactory.newInstance();
xs=factory.newSerializer();
xs.setOutput(output, "UTF-8");
xs.startDocument("UTF-8", true);
xs.startTag(null, "addresslist");
Iterator<LinkMan> iterator=all.iterator();
while(iterator.hasNext()){
LinkMan man=iterator.next();
xs.startTag(null, "linkman");
xs.startTag(null, "name");
xs.text(man.getName());
xs.endTag(null, "name");
xs.startTag(null, "email");
xs.text(man.getEmail());
xs.endTag(null, "email");
xs.endTag(null, "linkman");
}
xs.endTag(null, "addresslist");
xs.endDocument();
xs.flush();
} catch (Exception e) {
e.printStackTrace();
}
};
}
Main.java
package com.example.xmlpulldemo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=new File(Environment.getExternalStorageDirectory()+File.separator+"files/demo.xml");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
List<LinkMan> all=new ArrayList<LinkMan>();
for(int i=0;i<2;i++){
LinkMan man=new LinkMan();
man.setName("DanielHan"+i);
man.setEmail(i+"@qq.com");
all.add(man);
}
try {
XmlPullUtil util=new XmlPullUtil(new FileOutputStream(file), all);
util.save();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}else{
Toast.makeText(this, "没有sdcard", Toast.LENGTH_LONG).show();
}
}
}