Android 开发 解析XML文件数据 -- 接收的XML是String形式

接收到的数据是String类型,如下格式:

"1.0" encoding="UTF-8"?>2.0" xmlns="https://www.baidu.com/">花花0100"
  • 为了方便理解,断句如下:
<?xml version="1.0" encoding="UTF-8"?>
<Data version="2.0" xmlns="https://www.baidu.com/">
<name>花花</name>
<age>5</age>
<Progress>100</Progress>
</Date>

用法

XmlData xmlData = new XmlData();
XmlPer data2 = xmlData.getData("XML的数据(String)");
//XmlPer类里面就是分析的xml数据

写一个工具类方法,

package com.example.myapplication.utils;

import android.util.Log;
import android.util.Xml;
import com.example.myapplication.demo.model.data.XmlPer;
import org.xmlpull.v1.XmlPullParser;
import java.io.ByteArrayInputStream;

public class XmlData{

    public static XmlPer getData(String data1) throws Exception {
        ByteArrayInputStream inputstream = new ByteArrayInputStream(data1.getBytes());
        XmlPullParser xml = Xml.newPullParser();
        xml.setInput(inputstream, "UTF-8");
        int event = xml.getEventType();
        XmlPer xmlPer = new XmlPer();
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
                //开始解析文档
                case XmlPullParser.START_DOCUMENT:
                    Log.d("TAG", "开始解析-------------------");
                    break;
                case XmlPullParser.START_TAG:
                    Log.d("TAG", "解析中-------------------");
                    String value = xml.getName();
                    if (value.equals("Data")) {//XmlPer对象的初始化必须在这里初始化不然可能出现为null的现象
                    //......
                    } else if (value.equals("name")) {
                        xmlPer.setName(xml.nextText());
                    } else if (value.equals("age")) {
                        xmlPer.setAge(xml.nextText());
                    } else if (value.equals("Progress")) {
                        xmlPer.setProgress(xml.nextText());
                    } 
                    break;
                case XmlPullParser.END_TAG:
                    Log.d("TAG", "结束解析-------------------");
                    break;
            }
            //解析下一个对象
            event = xml.next();
        }
        return xmlPer;
    }
}
XmlPer类 也贴出来吧
public class XmlPer {
    private String name;
    private String age;
    private String Progress;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getProgress() {
        return Progress;
    }

    public void setProgress(String progress) {
        Progress = progress;
    }
}

  • 结束

如果是一个list集合,如下

<?xml version="1.0" encoding="UTF-8"?>
<Data version="2.0" xmlns="https://www.baidu.com/">
	<person id="1">
		<name>花花</name>
		<age>5</age>
		<Progress>100</Progress>
	</person>
	<person id="2">
		<name>牛牛</name>
		<age>6</age>
		<Progress>100</Progress>
	</person>
</Data>

写法如下:

package com.example.myapplication.utils;

import android.util.Log;
import android.util.Xml;
import com.example.myapplication.demo.model.data.XmlPer;
import org.xmlpull.v1.XmlPullParser;
import java.io.ByteArrayInputStream;

public class XmlData{

    public static List<XmlPer> getData(String data1) throws Exception {
        ByteArrayInputStream inputstream = new ByteArrayInputStream(data1.getBytes());
        XmlPullParser xml = Xml.newPullParser();
        xml.setInput(inputstream, "UTF-8");
        int event = xml.getEventType();
        XmlPer xmlPer = null;
        List<XmlPer> list=null;
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
                //开始解析文档
                case XmlPullParser.START_DOCUMENT:
                    list=new ArrayList<XmlPer>();
                    Log.d("TAG", "开始解析-------------------");
                    break;
                case XmlPullParser.START_TAG:
                    Log.d("TAG", "解析中-------------------");
                    String value = xml.getName();
                    if (value.equals("person")) {//XmlPer对象的初始化必须在这里初始化不然可能出现为null的现象
                    	xmlPer = new XmlPer();
                    } else if (value.equals("name")) {
                        xmlPer.setName(xml.nextText());
                    } else if (value.equals("age")) {
                        xmlPer.setAge(xml.nextText());
                    } else if (value.equals("Progress")) {
                        xmlPer.setProgress(xml.nextText());
                    } 
                    break;
                case XmlPullParser.END_TAG:
                 	if(xml.getName().equals("person")){
                         list.add(xmlPer);
                         xmlPer=null;
                     }
                    Log.d("TAG", "结束解析-------------------");
                    break;
            }
            //解析下一个对象
            event = xml.next();
        }
        return list;
    }
}

下面是String/InputStream/File之间的相互转换

String/InputStream/File之间的相互转换

  • String 转 InputStream
/**
 * 将str转换为inputStream
 * @param str
 * @return
 */
public static InputStream str2InputStream(String str) {
	ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes());
	return is;
}
  • InputStream 转 String
/**
 * 将inputStream转换为str
 * @param is
 * @return
 * @throws IOException
 */
public static String inputStream2Str(InputStream is) throws IOException {
	StringBuffer sb;
	BufferedReader br = null;
	try {
		br = new BufferedReader(new InputStreamReader(is));
		sb = new StringBuffer();
		String data;
		while ((data = br.readLine()) != null) {
			sb.append(data);
		}
	} finally {
		br.close();
	}
	return sb.toString();
}

InputStream 与 File之间转换

  • File 转 InputStream
/**
 * 将file转换为inputStream
 * @param file
 * @return
 * @throws FileNotFoundException
 */
public static InputStream file2InputStream(File file) throws FileNotFoundException {
	return new FileInputStream(file);
}
  • InputStream 转 File
/**
 * 将inputStream转化为file
 * @param is
 * @param file 要输出的文件目录
 */
public static void inputStream2File(InputStream is, File file) throws IOException {
	OutputStream os = null;
	try {
		os = new FileOutputStream(file);
		int len = 0;
		byte[] buffer = new byte[8192];

		while ((len = is.read(buffer)) != -1) {
			os.write(buffer, 0, len);
		}
	} finally {
		os.close();
		is.close();
	}
}

你可能感兴趣的:(android,xml,javascript)