用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

解析本地 服务器 xml

2013-05-15 作者: 蹲在牆頭等丶紅杏举报

[android]代码库

package com.service;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
 
import android.util.Xml;
 
 
import com.bean.Chart;
 
/**
 * 使用pull解析xml 2010-04-26
 *
 * @author leequer
 *
 */
public class ReadXmlByPullService {
 
    private static XmlPullParser xmlpull;
    private static Chart personList;
    public static int xmlType;
    private static InputStream inputStream;
    private static HttpURLConnection conn;
     
     
    public ReadXmlByPullService(int xmlType) {
        this.xmlType = xmlType;
 
    }
 
 
    /**
     * 0:解析本地 1:解析服务器str 2:解析服务器xml文件
     * @return
     */
 
    public static Chart  ReadXmlByPull(String path) throws Exception {
        xmlpull = Xml.newPullParser();
 
        if (0 == xmlType) {
            Xml_Sdcard(path);
        } else if (1 == xmlType) {
            Xml_Str(path);
        } else if (2 == xmlType) {
            Xml_Web(path);
        }
        XmlPull();
        return personList;
    }
 
    /**
     *
     * @param path_sdcard
     *            本地地址
     * @throws Exception
     */
    private static String Xml_Sdcard(String path) {
 
        try {
            File file = new File(path);
            inputStream = new FileInputStream(file);           
            xmlpull.setInput(inputStream, "utf-8");
             
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return path;
    }
    /**
     *
     * @param path_web
     *            服务器地址
     * @throws Exception
     */
    private static String Xml_Web(String path) {
 
        try {
            URL url = new URL(path);
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10 * 1000);
            conn.setRequestMethod("GET");
            inputStream = conn.getInputStream();
            xmlpull.setInput(inputStream, "utf-8");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return path;
    }
 
    /**
     *
     * @param path_str
     *            字符串地址
     * @return
     * @throws XmlPullParserException
     * @throws IOException
     * @throws Exception
     */
    private static String Xml_Str(String path) throws XmlPullParserException {
 
        String resultData = "";
 
        try {
            URL url = new URL(path);
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(5 * 1000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
 
            if (conn.getResponseCode() == 200) {
                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                String inputLine = null;
                while ((inputLine = buffer.readLine()) != null) {
                    resultData += inputLine + " ";
                }
            }
            xmlpull.setInput(new StringReader(resultData));
 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultData;
 
    }
 
    /**
     * 核心
     *
     * @throws Exception
     */
    private static void XmlPull() throws Exception {
 
        int eventCode = xmlpull.getEventType();
 
        while (eventCode != XmlPullParser.END_DOCUMENT) {
 
            switch (eventCode) {
            case XmlPullParser.START_DOCUMENT: {// 开始文档 new数组
                personList = new Chart();
                break;
            }
            case XmlPullParser.START_TAG: {
                if ("chart".equals(xmlpull.getName())) {
                    personList.setCaption(xmlpull.getAttributeValue(5));
                    personList.setxAxisName(xmlpull.getAttributeValue(7));
                    personList.setyAxisName(xmlpull.getAttributeValue(8));
                }
                if ("set".equals(xmlpull.getName())) {
                    personList.getX().add(xmlpull.getAttributeValue(0));
                    personList.getY().add(xmlpull.getAttributeValue(1));
                }
                break;
            }
 
            }
 
            eventCode = xmlpull.next();// 没有结束xml文件就推到下个进行解析
        }
    }
}

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...