Featured post
checking resources in xml and displaying icons for the resources in android -
hai every one. new android. in project had problems reading xml files. in xml have included audios , videos paths , want read xml file through code , want display images n view if there audio or video files. can body tel how read xml file. thanking in advance
ok first need create parser below code this:
public static void readtemplatefile(context context) {
/** include file checking */ try { xml_handler_template myexhan = new xml_handler_template(); inputstreamreader isr = new filereader( new file(environment.getexternalstoragedirectory().getpath() + "/library template.xml" )); xml_handler_template.context = context; saxparserfactory spf = saxparserfactory.newinstance(); saxparser sp = spf.newsaxparser(); xmlreader xr = sp.getxmlreader(); xr.setcontenthandler((contenthandler) myexhan); xr.parse(new inputsource(isr)); } catch (exception e) { toast.maketext(context, ">" + e.getmessage(), toast.length_long).show(); } }
you need handler class. in above example class called xml_handler_template.
filereader( new file(environment.getexternalstoragedirectory().getpath() + "/filepath/file.xml" ));
here xml_handler_class @ moment blank:
import org.xml.sax.attributes; import org.xml.sax.saxexception; import org.xml.sax.helpers.defaulthandler;
public class xml_handler_template extends defaulthandler{
public static context context = null; @override public void startdocument() throws saxexception { //this called when document first read } @override public void startelement(string namespaceuri, string localname, string qname, attributes atts) throws saxexception { //this called when new tag opened //localname holds tag name, value got //characters function @ end of class //the attributes each tag stored in atts array, can either handle attribute values here or pass information separate function handle them, if (atts.getlength()>0){ (int i=0;i<atts.getlength();i++){ addattrib(atts.getlocalname(i) , atts.getvalue(i)) ; } } } @override public void endelement(string namespaceuri, string localname, string qname) throws saxexception { //this called when tag closed } @override public void enddocument() throws saxexception { //this called when document closed } @override public void characters(char ch[], int start, int length) { //this value of tag read string value = new string( ch , start , length ); // may want include replaceall("\r","") , replaceall("\n","") remove hidden chars }
}
have play , see how on =0) passed context class whilst learning use toasts show me values being read.
- Get link
- X
- Other Apps
Comments
Post a Comment