解析代码
Java代码
publicstaticList URLReadXmlByPull(String xmlUrlPath)throwsException{
List listNews = newArrayList();;
News news = null;
URL url = newURL(xmlUrlPath);
//构建XmlPullParserFactory
XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
//获取XmlPullParser的实例
XmlPullParser xmlPullParser = pullParserFactory.newPullParser();
Log.i("PullParseXML","getXML......");
//设置输入流 xml文件装载器
xmlPullParser.setInput(url.openConnection().getInputStream(), "UTF-8");
//开始
Log.i("PullParseXML","PullParseXML....start....");
inteventType=xmlPullParser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
String nodeName=xmlPullParser.getName();
switch(eventType) {
caseXmlPullParser.START_DOCUMENT:
break;
caseXmlPullParser.START_TAG:
if("item".equals(nodeName)){
news = newNews();
}
if("title".equals(nodeName) && news !=null){
news.setTitle(xmlPullParser.nextText());
}
if("link".equals(nodeName) && news !=null){
news.setLink(xmlPullParser.nextText());
}
if("author".equals(nodeName) && news !=null){
news.setAuthor(xmlPullParser.nextText());
}
if("guid".equals(nodeName) && news !=null){
news.setGuid(xmlPullParser.nextText());
}
if("image".equals(nodeName) && news !=null){
news.setImage(xmlPullParser.nextText());
}
if("video".equals(nodeName) && news !=null){
news.setVideo(xmlPullParser.nextText());
}
if("category".equals(nodeName) && news !=null){
news.setCategory(xmlPullParser.nextText());
}
if("pubDate".equals(nodeName) && news !=null){
news.setPubDate(xmlPullParser.nextText());
}
if("comments".equals(nodeName) && news !=null){
news.setComments(xmlPullParser.nextText());
}
if("description".equals(nodeName) && news !=null){
news.setDescription(xmlPullParser.nextText());
}
break;
caseXmlPullParser.END_TAG:
if("item".equals(nodeName)){
listNews.add(news);
}
break;
default:
break;
}
eventType = xmlPullParser.next();
}
returnlistNews;
}
public static List URLReadXmlByPull(String xmlUrlPath) throws Exception{
List listNews = new ArrayList();;
News news = null;
URL url = new URL(xmlUrlPath);
//构建XmlPullParserFactory
XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
//获取XmlPullParser的实例
XmlPullParser xmlPullParser = pullParserFactory.newPullParser();
Log.i("PullParseXML", "getXML......");
//设置输入流 xml文件装载器
xmlPullParser.setInput(url.openConnection().getInputStream(), "UTF-8");
//开始
Log.i("PullParseXML", "PullParseXML....start....");
int eventType=xmlPullParser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
String nodeName=xmlPullParser.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if("item".equals(nodeName)){
news = new News();
}
if("title".equals(nodeName) && news != null){
news.setTitle(xmlPullParser.nextText());
}
if("link".equals(nodeName) && news != null){
news.setLink(xmlPullParser.nextText());
}
if("author".equals(nodeName) && news != null){
news.setAuthor(xmlPullParser.nextText());
}
if("guid".equals(nodeName) && news != null){
news.setGuid(xmlPullParser.nextText());
}
if("image".equals(nodeName) && news != null){
news.setImage(xmlPullParser.nextText());
}
if("video".equals(nodeName) && news != null){
news.setVideo(xmlPullParser.nextText());
}
if("category".equals(nodeName) && news != null){
news.setCategory(xmlPullParser.nextText());
}
if("pubDate".equals(nodeName) && news != null){
news.setPubDate(xmlPullParser.nextText());
}
if("comments".equals(nodeName) && news != null){
news.setComments(xmlPullParser.nextText());
}
if("description".equals(nodeName) && news != null){
news.setDescription(xmlPullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if("item".equals(nodeName)){
listNews.add(news);
}
break;
default:
break;
}
eventType = xmlPullParser.next();
}
return listNews;
}
使用时
Java代码
//从网络读取XML文件用Pull解析XML---解析jinghua2.xml(http://www.jinghua.cn/iphone/xml/bj.xml)
publicvoidtestPullRead3()throwsThrowable{
List persons = ReadXmlByPullService.URLReadXmlByPull("http://www.jinghua.cn/iphone/xml/bj.xml");
for(News person : persons){
Log.i(TAG, person.toString());
}
}
//从网络读取XML文件用Pull解析XML---解析jinghua2.xml(http://www.jinghua.cn/iphone/xml/bj.xml)
public void testPullRead3() throws Throwable{
List persons = ReadXmlByPullService.URLReadXmlByPull("http://www.jinghua.cn/iphone/xml/bj.xml");
for(News person : persons){
Log.i(TAG, person.toString());
}
}
实体
Java代码
packagecom.yangguangfu.xml.domain;
publicclassNews {
privateString title;
privateString link;
privateString author;
privateString guid;
privateString image;
privateString video;
privateString category;
privateString pubDate;
privateString comments;
privateString description;
publicString getTitle() {
returntitle;
}
publicvoidsetTitle(String title) {
this.title = title;
}
publicString getLink() {
returnlink;
}
publicvoidsetLink(String link) {
this.link = link;
}
publicString getAuthor() {
returnauthor;
}
publicvoidsetAuthor(String author) {
this.author = author;
}
publicString getGuid() {
returnguid;
}
publicvoidsetGuid(String guid) {
this.guid = guid;
}
publicString getImage() {
returnimage;
}
publicvoidsetImage(String image) {
this.image = image;
}
publicString getVideo() {
returnvideo;
}
publicvoidsetVideo(String video) {
this.video = video;
}
publicString getCategory() {
returncategory;
}
publicvoidsetCategory(String category) {
this.category = category;
}
publicString getPubDate() {
returnpubDate;
}
publicvoidsetPubDate(String pubDate) {
this.pubDate = pubDate;
}
publicString getComments() {
returncomments;
}
publicvoidsetComments(String comments) {
this.comments = comments;
}
publicString getDescription() {
returndescription;
}
publicvoidsetDescription(String description) {
this.description = description;
}
@Override
publicString toString() {
return"title="+ title +"guid="+ guid +",link="+ link
+ ",description="+ description +",image="+ image +",video="
+ video + ",category="+ category +",author="+ author
+ ",pubDate="+ pubDate +",comments="+ comments;
}
}