Professional Documents
Culture Documents
Lap Trinh Tren Android 3 4762 PDF
Lap Trinh Tren Android 3 4762 PDF
com
www.giaotrinhcntt.com
V d 6. Trnh x l SAX
import
org.developerworks.android.BaseFeedParser.*;
static
if
if
www.giaotrinhcntt.com
www.giaotrinhcntt.com
(localName.equalsIgnoreCase(DESCRIPTION)){
currentMessage.setDescription(builder.toString());
}
else
(localName.equalsIgnoreCase(PUB_DATE)){
currentMessage.setDate(builder.toString());
}
else
(localName.equalsIgnoreCase(ITEM)){
messages.add(currentMessage);
}
builder.setLength(0);
}
}
if
if
@Override
public void startDocument() throws SAXException {
super.startDocument();
messages = new ArrayList<Message>();
builder = new StringBuilder();
}
@Override
public
void
startElement(String
uri,
String
localName, String name,
Attributes attributes) throws SAXException
{
super.startElement(uri,
localName,
name,
attributes);
if (localName.equalsIgnoreCase(ITEM)){
this.currentMessage = new Message();
}
}
}
Lp
RssHandler
m
rng
lp
org.xml.sax.helpers.DefaultHandler. Lp ny cung cp cc thc thi
mc nh, khng thao tc cho tt c cc phng thc tng t cc s kin c to
ra bi trnh phn tch SAX. iu ny cho php cc lp con ch ghi chn ln cc
www.giaotrinhcntt.com
www.giaotrinhcntt.com
AndroidSaxFeedParser
extends
www.giaotrinhcntt.com
www.giaotrinhcntt.com
AndroidSaxFeedParser
extends
www.giaotrinhcntt.com
www.giaotrinhcntt.com
www.giaotrinhcntt.com
www.giaotrinhcntt.com
try {
Xml.parse(this.getInputStream(),
Xml.Encoding.UTF_8,
root.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
return messages;
}
}
Nh ha, m phn tch SAX mi khng s dng trnh x l SAX. Thay vo
n s dng cc lp t gi android.sax trong SDK. Cc lp ny cho php bn m
hnh ha cu trc ca ti liu XML ca bn v thm mt trnh nghe s kin nu
cn. Trong m trnh trn, bn khai bo rng ti liu ca bn s c mt phn t gc
c tn rss v rng phn t ny s c ba phn t con l channel. Tip n bn
ni rng channel s c ba phn t con c gi l ITEM v bn bt u gn cc
trnh nghe. i vi mi trnh nghe, bn s dng mt lp bn trong v danh
thc hin giao din bn quan tm (hoc EndElementListner hoc
EndTextElementListener). Ch khng cn phi theo di d liu k t.
Vic ny khng ch n gin hn m thc s cn hiu qu hn. Cui cng, khi bn
gi dn phng thc tin ch Xml.parse, by gi bn a vo trnh x l c to
ra t phn t gc.
Ton b m trnh trn trong V d 8 thuc loi ty chn. Nu bn thy thoi mi
vi m trnh phn tch SAX chun trong mi trng Java, th bn c th tch vo
. Nu bn mun th cc trnh bao bc tin li do Android SDK cung cp, bn
cng c th s dng n. Nu bn khng mun s dng SAX th sao y? Vn cn
c mt vi la chon khc. La chn u tin bn s thy l DOM.