Saturday, 15 January 2011

android - How to remove in my parse data Dom Parsing -



android - How to remove <p/> <p> <br/> <br> in my parse data Dom Parsing -

hello guys having problems parse info seems letters beingness parse want them remove

how in code, post below.

here xmlparser.java

public class xmlparser { // constructor public xmlparser() { } /** * getting xml url making http request * @param url string * */ public string getxmlfromurl(string url) { string xml = null; seek { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); xml = entityutils.tostring(httpentity); } grab (unsupportedencodingexception e) { e.printstacktrace(); } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } // homecoming xml homecoming xml; } /** * getting xml dom element * @param xml string * */ public document getdomelement(string xml){ document doc = null; documentbuilderfactory dbf = documentbuilderfactory.newinstance(); dbf.setcoalescing(true); dbf.setnamespaceaware(true); if (dbf.isnamespaceaware()==boolean.true) { dbf.setnamespaceaware(boolean.false); } seek { documentbuilder db = dbf.newdocumentbuilder(); inputsource = new inputsource(); is.setcharacterstream(new stringreader(xml)); doc = db.parse(is); } grab (parserconfigurationexception e) { log.e("error: ", e.getmessage()); homecoming null; } grab (saxexception e) { log.e("error: ", e.getmessage()); homecoming null; } grab (ioexception e) { log.e("error: ", e.getmessage()); homecoming null; } homecoming doc; } /** getting node value * @param elem element */ public final string getelementvalue( node elem ) { node child; if( elem != null){ if (elem.haschildnodes()){ for( kid = elem.getfirstchild(); kid != null; kid = child.getnextsibling() ){ if( child.getnodetype() == node.text_node || child.getnodetype() == node.cdata_section_node ){ homecoming child.getnodevalue(); } } } } homecoming ""; } /** * getting node value * @param element node * @param key string * */ public string getvalue(element item, string str) { nodelist n = item.getelementsbytagname(str); str = str.replaceall("<br/>"," \n"); homecoming this.getelementvalue(n.item(0)); } public final string getelemementvalue2 ( node elem) { node child; if( elem != null) { if (elem.haschildnodes()) { ( kid = elem.getfirstchild(); kid != null; kid = child.getnextsibling() ) { if(child.getnodetype() == node.cdata_section_node) { homecoming child.getnodevalue(); } } } } homecoming ""; } public string getvalue3(element item, string str) { nodelist n = item.getelementsbytagnamens("http://purl.org/rss/1.0/modules/content/",str ); string ses = this.getelemementvalue2(n.item(0)); string mim =ses.replaceall("(?s)\\<.*?\\>", " \n"); homecoming mim; } }

here main.java androidxmlparsingactivity

public class androidxmlparsingactivity extends listactivity { // static variables static final string url = "https://news.instaforex.com/news"; // xml node keys static final string key_item = "item"; // parent node static final string key_id = "id"; static final string key_title = "title"; static final string key_pubdate = "pubdate"; static final string key_description = "description"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); arraylist<hashmap<string, string>> menuitems = new arraylist<hashmap<string, string>>(); xmlparser parser = new xmlparser(); string xml = parser.getxmlfromurl(url); // getting xml document doc = parser.getdomelement(xml); // getting dom element nodelist nl = doc.getelementsbytagname(key_item); // looping through item nodes <item> (int = 0; < nl.getlength(); i++) { // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); element e = (element) nl.item(i); // adding each kid node hashmap key => value map.put(key_id, parser.getvalue(e, key_id)); map.put(key_title, parser.getvalue(e, key_title)); map.put(key_pubdate, parser.getvalue(e, key_pubdate)); map.put(key_description, parser.getvalue(e, key_description)); // adding hashlist arraylist menuitems.add(map); } // adding menuitems listview listadapter adapter = new simpleadapter(this, menuitems, r.layout.list_item, new string[] { key_title, key_description, key_pubdate }, new int[] { r.id.name, r.id.desciption, r.id.cost }); adapter.setnotifyonchange(true); setlistadapter(adapter); // selecting single listview item listview lv = getlistview(); lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string title = ((textview) view.findviewbyid(r.id.name)).gettext().tostring(); string pubdate = ((textview) view.findviewbyid(r.id.cost)).gettext().tostring(); string description = ((textview) view.findviewbyid(r.id.desciption)).gettext().tostring(); // starting new intent intent in = new intent(getapplicationcontext(), singlemenuitemactivity.class); in.putextra(key_title, title); in.putextra(key_pubdate, pubdate); in.putextra(key_description, description); startactivity(in); } }); } }

i wan parse these codes here in description , want stop parse info before

please link , check description.

check out how strip or escape html tags in android

you can utilize html.fromhtml(stringtoescape).tostring(); method escape tags whichever want.

edited:

try pass values of pudate & description getting hashmap showned below:

@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); arraylist<hashmap<string, string>> menuitems = new arraylist<hashmap<string, string>>(); xmlparser parser = new xmlparser(); string xml = parser.getxmlfromurl(url); // getting xml document doc = parser.getdomelement(xml); // getting dom element nodelist nl = doc.getelementsbytagname(key_item); // looping through item nodes <item> (int = 0; < nl.getlength(); i++) { // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); element e = (element) nl.item(i); // adding each kid node hashmap key => value map.put(key_id, parser.getvalue(e, key_id)); map.put(key_title, parser.getvalue(e, key_title)); map.put(key_pubdate, parser.getvalue(e, key_pubdate)); map.put(key_description, parser.getvalue(e, key_description)); // adding hashlist arraylist menuitems.add(map); } // adding menuitems listview listadapter adapter = new simpleadapter(this, menuitems, r.layout.list_item, new string[] { key_title, key_pubdate }, new int[] { r.id.name, r.id.cost }); setlistadapter(adapter); // selecting single listview item listview lv = getlistview(); lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string m_pubdate = menuitems.get(position).get(key_pubdate).tostring(); string m_description=menuitems.get(position).get(key_description).tostring(); system.out.println("pubdate==>"+m_pubdate+"\n description===>"+m_description); // starting new intent intent in = new intent(getapplicationcontext(), singlemenuitemactivity.class); in.putextra(key_pubdate, m_pubdate); in.putextra(key_description, m_description); startactivity(in); } }); }

i hope help you.

thanks.

android parsing dom xml-parsing

No comments:

Post a Comment