Pull解析完成XML文档的输出

前端之家收集整理的这篇文章主要介绍了Pull解析完成XML文档的输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


在AndroidManifest.xml中配置权限:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.li.xmlpull"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

<activity

android:name=".MyXMLPullDemo"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

在MyXMLPullDemo.java程序中

package org.lxh.demo;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

public class MyXMLPullDemo extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main);

if (!Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)) { // 不存在不操作

return; // 返回到程序的被调用

}

File file = new File(Environment.getExternalStorageDirectory()

+ File.separator + "liyewen" + File.separator

+ "test.xml"); // 要输出文件的路径

if (!file.getParentFile().exists()) { // 文件不存在

file.getParentFile().mkdirs() ; // 创建文件

}

List<LinkMan> all = new ArrayList<LinkMan>() ;

for (int x = 0; x < 3; x++) {

LinkMan man = new LinkMan() ;

man.setName("贾宝玉 - " + x ) ;

man.setEmail("jiabaoyu@163.com") ;

all.add(man) ;

}

OutputStream output = null ;

try {

output = new FileOutputStream(file) ;

new MyXMLPullUtil(output,all).save() ;

} catch (Exception e) {

}finally{

if (output != null) {

try {

output.close() ;

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

在MyXMLPullUtil.java中:

package org.lxh.demo;

import java.io.OutputStream;

import java.util.Iterator;

import java.util.List;

import org.xmlpull.v1.XmlPullParserFactory;

import org.xmlpull.v1.XmlSerializer;

public class MyXMLPullUtil {

private List<LinkMan> all = null;

private OutputStream output = null;

public MyXMLPullUtil(OutputStream output,List<LinkMan> all) {

this.output = output;

this.all = all;

}

public void save() throws Exception {

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

XmlSerializer xs = factory.newSerializer();

xs.setOutput(this.output,"UTF-8");

xs.startDocument("UTF-8",true);

xs.startTag(null,"addresslist");// 根元素

Iterator<LinkMan> iter = this.all.iterator();

while (iter.hasNext()) {

LinkMan man = iter.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();

}

}

在LinkMan.java中:

package org.lxh.demo;

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;

}

}

原文链接:https://www.f2er.com/xml/299879.html

猜你在找的XML相关文章