java - I am trying to parse an XML file, but I am having some trouble fixing the errors I am receiving -


i have been trying figure out how figure out how can solve errors receiving while trying parse xml file.

this mainactivity.java file:

package com.example.android;  import java.io.ioexception; import java.io.stringreader; import java.io.unsupportedencodingexception;  import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception;  import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import org.w3c.dom.document; import org.w3c.dom.node; import org.w3c.dom.nodelist; import org.xml.sax.inputsource; import org.xml.sax.saxexception; import org.xmlpull.v1.xmlpullparser;  import android.r.string; import android.app.activity; import android.os.bundle; import android.renderscript.element; import android.util.log; import android.view.menu; import android.view.window; import android.view.animation.animation; import android.view.animation.animationutils; import android.widget.imageview; import android.widget.textview;  public class mainactivity extends activity {      string playerid,            playerpd,            playertype,            xml;     defaulthttpclient httpclient;     httppost httppost;     httpresponse httpresponse;     httpentity httpentity;     document doc;     documentbuilderfactory dbf;     documentbuilder db;     inputsource inputsource;     nodelist n,              nl;     node child;     xmlpullparser parser;     static final string url = ".xml url witheld";     static final string players = "players"; //parent node     static final string player_pd = "playerpd";     static final string player_type = "playertype";           @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         //remove title bar         this.requestwindowfeature(window.feature_no_title);         }     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      public string getxmlfromurl(string url){         xml = null;         try{             //defaulthttpclient             httpclient = new defaulthttpclient();             httppost = new httppost (url);              httpresponse = httpclient.execute(httppost);             httpentity = httpresponse.getentity();             xml = entityutils.tostring(httpentity);                  } catch (unsupportedencodingexception e) {             e.printstacktrace();         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e){             e.printstacktrace();         }         //return xml         return xml;      }      public document getdomelement(string xml){         doc = null;         dbf = documentbuilderfactory.newinstance();         try{             db = dbf.newdocumentbuilder();              inputsource = new inputsource();             inputsource.setcharacterstream(new stringreader (xml));              doc = db.parse(inputsource);         } catch (parserconfigurationexception e) {             log.e("error: ",e.getmessage());             return null;         } catch (saxexception e) {             log.e("error: ",e.getmessage());             return null;         } catch (ioexception e) {             log.e("error: ",e.getmessage());             return null;         }          //return dom         return doc;          }      public string getvalue(element item, string str){         n = ((document) item).getelementsbytagname(str);         return this.getelementvalue(n.item(0));          }      private final string getelementvalue(node elem){         // todo auto-generated method stub         if(elem.haschildnodes()){             for(child = elem.getfirstchild(); child != null; child = child.getnextsibling()){                 if(child.getnodetype() == node.text_node){                     return child.getnodevalue();                 }             }         }         return null;             }      public void parsexml(){        xml = parser.getxmlfromurl(url); //getting xml 

on line, line 149, receive error says"the method getxmlfromurl(string) undefined type xmlpullparser"

      doc = parser.getdomelement(xml); //getting dom element 

and, on line, line 150, receive error says, "the method getdomelement(string) undefined type xmlpullparser"

      nl = doc.getelementsbytagname(players);        (int = 0; < nl.getlength(); i++){       playerpd = parser.getvalue(e, player_pd); //playerpd child value 

also, on line , 1 follows it, lines 155 & 156, receive error says, "e cannot resolved variable"

      playertype = parser.getvalue(e, player_type); //player type child value }  } } 

if helps, layout of xml file trying parse this:

-<players>   -<player id="">     <playertype></playertype>    </player>  </players> 

any appreciated, , in advance.

remove parser.

public void parsexml(){        xml = getxmlfromurl(url); //getting xml 

or can create class xmlparser , add getxmlfromurl function inside new class

similarly other functions

doc = getdomelement(xml); //getting dom element 

your lines 155 & 156 not in full source code i'm not sure context.


so you've mentioned original tutorial. tutorial means need create new file xmlparser.java containing new class called xmlparser

public class xmlparser { } 

then add functions class

public class xmlparser {     public string getxmlfromurl(string url) {         string xml = null;          try {             // defaulthttpclient             defaulthttpclient httpclient = new defaulthttpclient();             httppost httppost = new httppost(url);              httpresponse httpresponse = httpclient.execute(httppost);             httpentity httpentity = httpresponse.getentity();             xml = entityutils.tostring(httpentity);          } catch (unsupportedencodingexception e) {             e.printstacktrace();         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }         // return xml         return xml;     } } 

so on , forth, adding other functions class. can use class mainactivity class.


as e cannot resolved variable, there's missing line in web page.

nodelist nl = doc.getelementsbytagname(key_item);  // looping through item nodes <item>      (int = 0; < nl.getlength(); i++) {     element e = (element)nl.item(i); // >>> missing line in sample <<<     string name = parser.getvalue(e, key_name); // name child value     string cost = parser.getvalue(e, key_cost); // cost child value     string description = parser.getvalue(e, key_desc); // description child value } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -