package
com.example.utility;
import
org.xml.sax.Attributes;
import
org.xml.sax.SAXException;
import
org.xml.sax.helpers.DefaultHandler;
import
android.R.integer;
public
class
BookHandle
extends
DefaultHandler {
BookFeed bookFeed;
BookItem bookItem;
final
int
Book_Country =
1
;
final
int
Book_Title =
2
;
final
int
Book_Artist =
3
;
final
int
Book_Price =
4
;
int
curState =
0
;
public
BookHandle() {
}
public
BookFeed getBookFeed() {
return
bookFeed;
}
@Override
public
void
startDocument()
throws
SAXException
{
bookFeed =
new
BookFeed();
bookItem =
new
BookItem();
}
@Override
public
void
endDocument()
throws
SAXException
{
}
@Override
public
void
startElement(String uri, String localName, String qName, Attributes attributes)
throws
SAXException {
if
(localName.equals(
"books"
)) {
curState =
0
;
return
;
}
if
(localName.equals(
"book"
)) {
bookItem =
new
BookItem();
return
;
}
if
(localName.equals(
"country"
)) {
curState = Book_Country;
return
;
}
if
(localName.equals(
"title"
)) {
curState = Book_Title;
return
;
}
if
(localName.equals(
"artist"
)) {
curState = Book_Artist;
return
;
}
if
(localName.equals(
"price"
)) {
curState = Book_Price;
return
;
}
curState =
0
;
}
@Override
public
void
endElement(String uri, String localName,String qName)
throws
SAXException
{
if
(localName.equals(
"book"
)) {
bookFeed.addBookItem(bookItem);
return
;
}
}
@Override
public
void
characters(
char
[] ch,
int
start,
int
length)
throws
SAXException {
String str =
new
String(ch,start,length);
switch
(curState) {
case
Book_Country:
bookItem.setName(str);
curState =
0
;
break
;
case
Book_Title:
bookItem.setTitle(str);
curState =
0
;
break
;
case
Book_Artist:
bookItem.setArtist(str);
curState =
0
;
break
;
case
Book_Price:
bookItem.setPrice(str);
curState =
0
;
break
;
default
:
return
;
}
}
}