android xml解析
一:模板1
1.1xml格式如下
1.2解析类如下:SAXGameService.java源码
package
com.XMLhandler; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import
org.xml.sax.Attributes; import
org.xml.sax.SAXException; import
org.xml.sax.helpers.DefaultHandler; import
com.yxyun.domain.Game; /** * @Author BraveStarr * @QQ 1733259520 * * 使用时如下: * final SAXGameService saxGameService = new SAXGameService(); * // 使用类装载器获取流数据 * new Thread(new Runnable() { * @Override * public void run() { * try { * InputStream inStream = InputStreamUtil * .getStreamContent("http://game.yxyun.com/WS/WS_Bestv_VOS.php?uid=387881&message=gamemall"); * List<Game> games = saxGameService.readXml(inStream); * Message message = new Message(); * message.what = REQUEST_HOME_LISTINFO_SUCCESS; * // 得到结果数据解析成getRemoteData()中的数据 * message.obj = getRemoteData(games); * mHandler.sendMessage(message); * } catch (Exception e) { * Message message = new Message(); * message.what = XML_PARSER_ERROR; * // 得到结果数据解析成getRemoteData()中的数据 * mHandler.sendMessage(message); * } * } * }).start(); */ public
class SAXGameService { public
List<Game> readXml(InputStream inStream) throws
Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxParser = spf.newSAXParser(); // 创建解析器 // 设置解析器相关特性,http://xml.org/sax/features/namespace = true // 表示开启命名空间特性 // saxParser.setProperty("http://xml.org/sax/features/namespace", true); GameDefaultHandler handler = new
GameDefaultHandler(); saxParser.parse(inStream, handler); inStream.close(); return
handler.getGames(); } private
final class GameDefaultHandler extends
DefaultHandler { private
List<Game> games = null ; private
Game game = null ; private
String tag = null ; @Override public
void startDocument() throws
SAXException { games = new
ArrayList<Game>(); } @Override public
void startElement(String uri, String localName, String qName, Attributes attributes) throws
SAXException { if
( "gameinfo" .equals(localName)) { game = new
Game(); } tag = localName; } @Override public
void endElement(String uri, String localName, String qName) throws
SAXException { if
( "gameinfo" .equals(localName) && game != null ) { games.add(game); game = null ; } tag = null ; } @Override public
void characters( char [] ch, int
start, int
length) throws
SAXException { if
(tag != null ) { String data = new
String(ch, start, length); if
( "ispack" .equals(tag)) { game.setIspack(data); } else
if ( "gameid" .equals(tag)) { game.setGameid(data); } else
if ( "gameName" .equals(tag)) { game.setGameName(data); } else
if ( "price" .equals(tag)) { game.setPrice(Integer.parseInt(data)); } else
if ( "days" .equals(tag)) { game.setDays(Integer.parseInt(data)); } else
if ( "is_buy" .equals(tag)) { game.setIs_buy(data); } else
if ( "type" .equals(tag)) { game.setType(data); } else
if ( "language" .equals(tag)) { game.setLanguage(data); } else
if ( "gameimage" .equals(tag)) { game.setGameimage(data); } else
if ( "gamehot" .equals(tag)) { game.setGamehot(data); } else
if ( "introduction" .equals(tag)) { game.setIntroduction(data); } else
if ( "gamevideo" .equals(tag)) { game.setGamevideo(data); } } } /** * @return the games */ public
List<Game> getGames() { return
games; } } } |
Game.java如下
package
com.yxyun.domain; import android.text.TextUtils; public class Game { private
String ispack = "0" ; //是否为礼包,"1"表示礼包、"0"表示游戏 private
String gameid; //游戏或礼包id,ispack为礼包时,对应礼包id,否则对应游戏id private
String gameName; //游戏名或礼包名 private
Integer price= 0 ; //游戏或礼包价格 private
Integer days= 0 ; //游戏或礼包服务期 private
String is_buy; //游戏或礼包是否已经购买 private
String type= "无" ; //(游戏)所属礼包类型 private
String language; //(游戏)语言 private
String gameimage; //游戏或礼包图片路径 private
String gamehot; //游戏人气 private
String introduction; //游戏介绍 private
String gamevideo; //游戏视频 public
String getGamevideo() { return
gamevideo; } public
void setGamevideo(String gamevideo) { this .gamevideo = gamevideo; } public
String getGamehot() { return
gamehot; } public
void setGamehot(String gamehot) { this .gamehot = gamehot; } public
String getIntroduction() { return
introduction; } public
void setIntroduction(String introduction) { this .introduction = introduction; } public
String getIspack() { return
ispack; } public
void setIspack(String ispack) { this .ispack = ispack; } public
String getGameid() { return
gameid; } public
void setGameid(String gameid) { this .gameid = gameid; } public
String getGameName() { return
gameName; } public
void setGameName(String gameName) { this .gameName = gameName; } public
Integer getPrice() { return
price; } public
void setPrice(Integer price) { this .price = price; } public
Integer getDays() { return
days; } public
void setDays(Integer days) { this .days = days; } public
String getIs_buy() { return
is_buy; } public
void setIs_buy(String is_buy) { this .is_buy = is_buy; } public
String getType() { return
type; } public
void setType(String type) { if (!TextUtils.isEmpty(type)){ this .type = type; } } public
String getLanguage() { return
language; } public
void setLanguage(String language) { this .language = language; } public
String getGameimage() { return
gameimage; } public
void setGameimage(String gameimage) { this .gameimage = gameimage; } } |
InputStreamUtil.java类如下
package
com.utils;
import
java.io.ByteArrayInputStream;
import
java.io.InputStream;
import
java.net.HttpURLConnection;
import
java.net.URL;
public
class
InputStreamUtil
{
/**
*
获取输入流
*
@param urlpath 文件路径
*
@return
*
@throws Exception
*/
public
static
InputStream getStreamContent(String
urlpath)
throws
Exception
{
URL url =
new
URL(urlpath);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setRequestMethod(
"GET"
);
//
设置请求方式,默认为"GET"
conn.setConnectTimeout(
6
*
1000
);
// 设置请求超时
// 请求图片或网页地址如果成功,返回响应码为200
if
(conn.getResponseCode()
==
200
)
{
return
conn.getInputStream();
}
return
null
;
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。