android - Java handling XML using SAX -
i've got xml need parse given structure:
<?xml version="1.0" encoding="utf-8"?> <root> <tag label="idad"> <child label="text">text</child> </tag> <tag label="idnumpage"> <child label1="text" label2="text">text</child> </tag> </root>
i use sax parser parse it:
rootelement root=new rootelement("root"); android.sax.element page_info=root.getchild("tag").getchild("child"); page_info.setstartelementlistener(new startelementlistener() { @override public void start(attributes attributes) { /*---------------*/ } });
i want read second "tag" element attributes(label1
, label2
), startelementlistener reads first tag, because have same structure , attributes(those label="idad"
, label="idnumpage"
) distinguish them. how tell startelementlistener process second <tag>
element?
if stuck startelementlistener
-way, should set listener tag
element, , when it's label
equals "idnumpage" set flag, other startelementlistener
you've set on child
element should read.
update
below sample of how using these listeners:
android.sax.element tag = root.getchild("tag"); final starttagelementlistener listener = new starttagelementlistener(); tag.setstartelementlistener(listener); android.sax.element page_info = tag.getchild("child"); page_info.setstartelementlistener(new startelementlistener() { @override public void start(attributes attributes) { if (listener.readnow()) { //todo: in tag label="idnumpage" } } });
and starttagelementlistener
implemented readnow
getter, let know when read child
tag's attributes:
public final class starttagelementlistener implements startelementlistener { private boolean doreadnow = false; @override public void start(attributes attributes) { doreadnow = attributes.getvalue("label").equals("idnumpage"); } public boolean readnow() { return doreadnow; } }
ps: did consider using org.xml.sax.helpers.defaulthandler
implementation task?
Comments
Post a Comment