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文件就推到下个进行解析 |
} |
} |
} |