java - Parsing the XML file has got error -
does find problem why no xml file not parsing following code.... xml format can seen here.
i tried test line line using log.i("your string goes here", xml); function unable see execution of code when loop starts....
i have used splash activity in asynctask() function executed in background , listactivity used display domparser activity output.... me out problem @ all.... thank in advance
package com.wfwf.everestnewsapp.parser; import java.net.malformedurlexception; import java.net.url; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import org.jsoup.jsoup; import org.jsoup.select.elements; import org.w3c.dom.document; import org.w3c.dom.node; import org.w3c.dom.nodelist; import org.xml.sax.inputsource; import android.widget.toast; public class domparser { private rssfeed _feed = new rssfeed(); public rssfeed parsexml(string xml) { url url = null; try { url = new url(xml); } catch (malformedurlexception e1) { e1.printstacktrace(); } try { // create required instances documentbuilderfactory dbf; dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder(); // parse xml document doc = db.parse(new inputsource(url.openstream())); doc.getdocumentelement().normalize(); // <item> tags. nodelist nl = doc.getelementsbytagname("item"); int length = nl.getlength(); (int = 0; < length; i++) { node currentnode = nl.item(i); rssitem _item = new rssitem(); nodelist nchild = currentnode.getchildnodes(); int clength = nchild.getlength(); // required elements each item // ishwor changed code j=0 , j= j+1 (int j = 0; j < clength; j = j + 1) { node thisnode = nchild.item(j); string thestring = null; /*//ishwor changed if (thisnode != null && thisnode.getfirstchild() != null) { thestring = thisnode.getfirstchild().getnodevalue(); } */ string nodename = thisnode.getnodename(); //thestring = nchild.item(j).getfirstchild().getnodevalue(); if(nchild.item(j).getfirstchild().getnodevalue()!=null){ //if (thestring != null) { //string nodename = thisnode.getnodename(); if ("title".equals(nodename)) { // node name equals 'title' set node // value title in rssitem. _item.settitle(thestring); } else if ("description".equals(nodename)) { _item.setdescription(thestring); // parse html description image url string html = thestring; org.jsoup.nodes.document dochtml = jsoup.parse(html); elements imgele = dochtml.select("img"); _item.setimage(imgele.attr("src")); } else if ("pubdate".equals(nodename)) { // replace plus , zero's in date // empty string string formateddate = thestring.replace(" +0000", ""); _item.setdate(formateddate); } } } // add item list _feed.additem(_item); } } catch (exception e) { } // return final feed once items added rssfeed // object(_feed). return _feed; } }
private document getdocument(inputstream stream) { document doc = null; try { documentbuilderfactory dbfactory = documentbuilderfactory .newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); if (stream != null) { doc = dbuilder.parse(stream); doc.getdocumentelement().normalize(); } else { } } catch (parserconfigurationexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (saxexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (exception e) { e.printstacktrace(); // todo: handle exception } return doc; } private string gettagvalue(string stag, element eelement) { string value = ""; try { nodelist nllist = eelement.getelementsbytagname(stag).item(0) .getchildnodes(); (int = 0; < nllist.getlength(); i++) { node nvalue = nllist.item(i); if (nvalue != null) { value = value + nvalue.getnodevalue().trim(); } } } catch (exception e) { } return value; } public entitybean parsedologin(string requestoutput) { entitybean response = null; inputstream stream = (inputstream) new bytearrayinputstream( requestoutput.getbytes()); document doc = getdocument(stream); nodelist nodelist = doc.getelementsbytagname("starttag"); (int = 0; < nodelist.getlength(); i++) { node nnode = nodelist.item(i); element ielement = (element) nnode; response = new entitybean(); string result = new string(gettagvalue("result", ielement)); response.setresult(result); string msg = new string(gettagvalue("message", ielement)); response.setmsg(msg); } return response; }
Comments
Post a Comment