Upload
trung-pham
View
226
Download
0
Embed Size (px)
Citation preview
8/3/2019 Lap Trinh Tren Android 3 4762 Diendandaihoc.vn 04505303112011
1/6
V d 6. Trnh x l SAX
import staticorg.developerworks.android.BaseFeedParser.*;
public class RssHandler extends DefaultHandler{private List messages;private Message currentMessage;private StringBuilder builder;
public List getMessages(){return this.messages;
}
@Overridepublic void characters(char[] ch, int start, int
length)throws SAXException {
super.characters(ch, start, length);
builder.append(ch, start, length);}
@Overridepublic void endElement(String uri, String
localName, String name)throws SAXException {
super.endElement(uri, localName, name);if (this.currentMessage != null){
if (localName.equalsIgnoreCase(TITLE)){
currentMessage.setTitle(builder.toString());} else if
(localName.equalsIgnoreCase(LINK)){
currentMessage.setLink(builder.toString());} else if
8/3/2019 Lap Trinh Tren Android 3 4762 Diendandaihoc.vn 04505303112011
2/6
(localName.equalsIgnoreCase(DESCRIPTION)){
currentMessage.setDescription(builder.toString());} else if
(localName.equalsIgnoreCase(PUB_DATE)){
currentMessage.setDate(builder.toString());} else if
(localName.equalsIgnoreCase(ITEM)){messages.add(currentMessage);
}builder.setLength(0);
}}
@Override
public void startDocument() throws SAXException {super.startDocument();messages = new ArrayList();builder = new StringBuilder();
}
@Overridepublic 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 lporg.xml.sax.helpers.DefaultHandler. Lp ny cung cp cc thc thimc nh, khng thao tc cho tt c cc phng thc tng t cc s kin c tora bi trnh phn tch SAX. iu ny cho php cc lp con ch ghi chn ln cc
8/3/2019 Lap Trinh Tren Android 3 4762 Diendandaihoc.vn 04505303112011
3/6
phng thc khi cn thit. RssHandlerc mt API b sung, getMessages.Ci ny tr v danh sch cc i tng Messagem trnh x l thu thp c khin nhn cc s kin t trnh phn tch SAX. N c hai bin trong khc, mt lcurrentMessagecho th hin Messageang c phn tch, v mt l bin
StringBuildergi l builderlu tr d liu k t t cc nt vn bn. Ccbin ny u c bt u khi phng thc startDocumentc dn ra khitrnh phn tch gi s kin tng ng cho trnh x l.
Hy xem phng thc startElement trong V d 6. Phng thc ny c gimi khi bt gp th m trong ti liu XML. Bn ch cn quan tm khi no th lth ITEM. Trong trng hp , bn to ra mt Messagemi. By gi hy nhnvo phng thc characters. Phng thc ny c gi ra khi bt gp d liuk t t cc nt vn bn. D liu d dng c thm vo bin builder. Cui
cng hy xem phng thc endElement. Phng thc ny c gi ra khi btgp th kt thc. i vi cc th tng ng vi cc c tnh ca mt Message,ging nh TITLE v LINK, c tnh thch hp c thit t trncurrentMessages dng d liu t bin builder. Nu th kt thc l mtITEM, th currentMessagethm vo danh sch Messages. y l s phn tchSAX rt in hnh; y khng c g l duy nht i vi Android. V th nu bn
bit cch vit mt trnh phn tch SAX Java, th bn bit cch vit mt trnh phntch SAX Android. Tuy nhin, Android SDK c b sung thm mt s tnh nngthun tin vo SAX.
Phn tch SAX d dng hn
Android SDK c cha mt lp tin ch c gi l android.util.Xml. V d7trnh by cch ci t mt trnh phn tch SAX vi cng lp tin ch nh th.
V d 7. Trnh phn tch SAX Android
public class AndroidSaxFeedParser extendsBaseFeedParser {
public AndroidSaxFeedParser(String feedUrl) {super(feedUrl);
}
8/3/2019 Lap Trinh Tren Android 3 4762 Diendandaihoc.vn 04505303112011
4/6
public List parse() {
RssHandler handler = new RssHandler();try {
Xml.parse(this.getInputStream(),Xml.Encoding.UTF_8, handler);
} catch (Exception e) {throw new RuntimeException(e);
}return handler.getMessages();
}
}
Lu l lp ny vn s dng trnh x l SAX chun, v n gin bn s dngli RssHandlernh trong V d 7 trn. Vic c th s dng li trnh x lSAX rt tt, nhng n vn c i cht phc tp v m trnh. Bn c tng tng,nu bn phi phn tch mt ti liu XML phc tp hn rt nhiu, trnh phn tch cth tr thnh mnh t mu m cho cc li. V d, hy xem li phng thcendElement trong V d 6. Lu cch phng thc ny kim tra nh th nonu currentMessagec gi tr khng trc khi n c ci t cc thuc tnh?By gi hy nhn vo XML mu trong V d 4. Lu rng c cc th TITLEv
LINKnm ngoi cc th
ITEM. l l do ti sao kim tra gi tr khng ca
vo. Nu khng th th TITLE u tin c th gy ra mtNullPointerException. Android bao gm c bin th SAX API ca chnhn (xem V d 8) loi b yu cu bn phi vit trnh x l SAX ca chnh bn.
V d 8. Trnh phn tch SAX Android n gin
public class AndroidSaxFeedParser extendsBaseFeedParser {
public AndroidSaxFeedParser(String feedUrl) {super(feedUrl);
}
public List parse() {final Message currentMessage = new Message();
8/3/2019 Lap Trinh Tren Android 3 4762 Diendandaihoc.vn 04505303112011
5/6
RootElement root = new RootElement("rss");final List messages = new
ArrayList();Element channel = root.getChild("channel");
Element item = channel.getChild(ITEM);item.setEndElementListener(new
EndElementListener(){public void end() {
messages.add(currentMessage.copy());}
});
item.getChild(TITLE).setEndTextElementListener(newEndTextElementListener(){
public void end(String body) {currentMessage.setTitle(body);
}});
item.getChild(LINK).setEndTextElementListener(newEndTextElementListener(){
public void end(String body) {
currentMessage.setLink(body);}
});
item.getChild(DESCRIPTION).setEndTextElementListener(new
EndTextElementListener(){public void end(String body) {
currentMessage.setDescription(body);}
});
item.getChild(PUB_DATE).setEndTextElementListener(new
EndTextElementListener(){public void end(String body) {
currentMessage.setDate(body);}
});
8/3/2019 Lap Trinh Tren Android 3 4762 Diendandaihoc.vn 04505303112011
6/6
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 nucn. Trong m trnh trn,bn khai bo rng ti liu ca bn s c mt phn t gcc tn rssv rng phn t ny s c ba phn t con l channel. Tip n bnni rng channels c ba phn t con c gi l ITEMv bn bt u gn cctrnh nghe. i vi mi trnh nghe, bn s dng mt lp bn trong v danh thc hin giao din bn quan tm (hoc EndElementListner hocEndTextElementListener). 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 bngi dnphng thc tin ch Xml.parse, by gi bn a vo trnh x l c tora t phn t gc.
Ton b m trnh trn trong V d 8thuc loi ty chn. Nu bn thy thoi mivi 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, bncng c th s dng n. Nu bn khng mun s dng SAX th sao y? Vn cnc mt vi la chon khc. La chn u tin bn s thy l DOM.