public
class
BookHandler
extends
DefaultHandler {
private
static
List<Book> bookList =
null
;
private
static
String tagName;
private
Book book =
null
;
public
List<Book> getBooks() {
return
bookList;
}
@Override
public
void
startDocument()
throws
SAXException {
super
.startDocument();
}
public
BookHandler() {
super
();
bookList =
new
ArrayList<Book>();
}
@Override
public
void
endDocument()
throws
SAXException {
super
.endDocument();
}
@Override
public
void
startElement(String uri, String localName, String qName,
Attributes attributes)
throws
SAXException {
tagName = localName;
if
(localName.equals(
"Book"
)) {
book =
new
Book();
book.setName(attributes.getValue(
"name"
));
}
}
@Override
public
void
endElement(String uri, String localName, String qName)
throws
SAXException {
if
(localName.equals(
"Book"
)) {
bookList.add(book);
}
}
@Override
public
void
characters(
char
[] ch,
int
start,
int
length)
throws
SAXException {
String value =
new
String(ch, start, length).trim();
if
(value !=
null
&& !
""
.equals(value) && !
"\n"
.equals(value)) {
switch
(tagName) {
case
"Title"
:
Log.i(
"titleString"
, value);
book.setTitle(value);
}
}
}
}