-------------------------------------------------------------------------------domain
package com.sforus.TextShangJi.domain;public class Book {private String title;private String author;private String isbn13;private String publisher;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getIsbn13() {return isbn13;}public void setIsbn13(String isbn13) {this.isbn13 = isbn13;}public String getPublisher() {return publisher;}public void setPublisher(String publisher) {this.publisher = publisher;}}
---------------------------------------------------------------
下面是處理xml
--------------------------------------------------------------
package com.sforus.TextShangJi.util;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import com.sforus.TextShangJi.domain.Book;import android.content.Context;import android.util.Xml;import com.google.gdata.client.douban.DoubanService;import net.htmlparser.jericho.Element;import net.htmlparser.jericho.Source;/** * 訪問資料庫,擷取書本 * * @author woops * */public class ConnectDataBase {private Book book;public List<Book> getNewBooks() throws Exception { List<Book> books = null; String path = "http://api.douban.com/book/subjects?tag=android&start-index=1&max-results=5";URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);InputStream inStream = conn.getInputStream();XmlPullParser parser = Xml.newPullParser();parser.setInput(inStream, "UTF-8");int eventType = parser.getEventType();// 產生第一個事件while (eventType != XmlPullParser.END_DOCUMENT) {boolean s=false;switch (eventType) {case XmlPullParser.START_DOCUMENT:books = new ArrayList<Book>();break;case XmlPullParser.START_TAG: if ("entry".equals(parser.getName())) { book = new Book(); } if (book!=null) { if ("title".equals(parser.getName())) { String title=parser.nextText().toString(); book.setTitle(title); } if ("name".equals(parser.getName())){
String name=parser.nextText().toString(); book.setAuthor(name); } if ("name".equals(parser.getName()==null)) { String name=null; book.setAuthor(name); } if("attribute".equals(parser.getName())){ if ("isbn13".equals(parser.getAttributeValue(0))) { book.setIsbn13( parser.nextText()); }else if ("publisher".equals(parser.getAttributeValue(0))) { book.setPublisher( parser.nextText()); } } } break; case XmlPullParser.END_TAG: if ("entry".equals(parser.getName())&& eventType == XmlPullParser.END_TAG ) { books.add(book); book=null; } break; } eventType=parser.next(); } return books;
}}