android - Sax parser with string xml + malformation error -


i trying read data xml string , set respective tag element using setter getter method xml shows malformation error in xml file. doing wrong here code.

in oncreate..

    saxhelper2 sh = null;      try {          sh = new saxhelper2(newxml);      } catch (malformedurlexception e) {         e.printstacktrace();     }       sh.parsecontent("");      return null;  } 

}

 /*   *   */   class saxhelper2 {       private string data;     stringbuffer chars = new stringbuffer();       public saxhelper2(string xmlstring) throws malformedurlexception {         this.data = new string(xmlstring);     }       defaulthandler handler = new defaulthandler();      public rsshandler parsecontent(string parsecontent) {         rsshandler df = new rsshandler();          try {              saxparserfactory factory = saxparserfactory.newinstance();     saxparser saxparser = factory.newsaxparser();     saxparser.parse(new inputsource(newxml), new rsshandler());          } catch (exception e) {             e.printstacktrace();         }          return df;     }       class rsshandler extends defaulthandler {          private comptepost currentpost = new comptepost();          stringbuffer chars = new stringbuffer();          public void startelement(string uri, string localname, string qname, attributes atts) {              chars = new stringbuffer();             if (localname.equalsignorecase("comptes")) {              }         }       defaulthandler handler = new defaulthandler() {          @override         public void endelement(string uri, string localname, string qname)                 throws saxexception {              if (localname.equalsignorecase("numcompte")                     && currentpost.getnumcomtpe() == null) {                 system.out.println("post: "+currentpost.getnumcomtpe());                 log.i("numcompte", currentpost.getnumcomtpe());                 currentpost.setnumcomtpe(chars.tostring());               }             if (localname.equalsignorecase("authcompte")                     && currentpost.getauthcompte() == null) {                 currentpost.setauthcompte(chars.tostring());              }             if (localname.equalsignorecase("typecompte")                     && currentpost.gettypecompte() == null) {                 currentpost.settypecompte(chars.tostring());               }             if (localname.equalsignorecase("libellecompte")                     && currentpost.getlibellecompte()== null) {                 currentpost.setlibellecompte(chars.tostring());             }              if (localname.equalsignorecase("soldecompte")                     && currentpost.getsoldecompte() == null) {                 currentpost.setsoldecompte(chars.tostring());             }                if (localname.equalsignorecase("devisecompte")                     && currentpost.getdevisecompte() == null) {                 currentpost.setdevisecompte(chars.tostring());               }             if (localname.equalsignorecase("datesolde")                     && currentpost.getdatesolde()== null) {                 currentpost.setdatesolde(chars.tostring());             }              if (localname.equalsignorecase("droitvirement")                     && currentpost.getdroitvirement()== null) {                 currentpost.setdroitvirement(chars.tostring());             }               if (localname.equalsignorecase("cartebancaire")                     && currentpost.getcartebancaire()== null) {                 currentpost.setcartebancaire(chars.tostring());             }              if (localname.equalsignorecase("debitmin")                     && currentpost.getdebitmin()== null) {                 currentpost.setdebitmin(chars.tostring());             }               if (localname.equalsignorecase("debitmax")                     && currentpost.getdebitmax()== null) {                 currentpost.setdebitmax(chars.tostring());             }               if (localname.equalsignorecase("creditmin")                     && currentpost.getcreditmin()== null) {                 currentpost.setcreditmin(chars.tostring());             }               if (localname.equalsignorecase("creditmax")                     && currentpost.getcreditmax()== null) {                 currentpost.setcreditmax(chars.tostring());             }              if (localname.equalsignorecase("echeancemax")                     && currentpost.getecheancemax()== null) {                 currentpost.setecheancemax(chars.tostring());             }                if (localname.equalsignorecase("comptes")) {                 postlist.add(currentpost);                 currentpost = new comptepost();             }          }           @override         public void characters(char ch[], int start, int length) {             chars.append(new string(ch, start, length));         }       };      }   } 

java.io.ioexception: couldn't open caused by: java.net.malformedurlexception: protocol not found:

04-05 15:24:52.699: w/system.err(4784):     @ org.apache.harmony.xml.expatparser.openurl(expatparser.java:760) 04-05 15:24:52.703: w/system.err(4784):     @ org.apache.harmony.xml.expatreader.parse(expatreader.java:289) 04-05 15:24:52.707: w/system.err(4784):     @ javax.xml.parsers.saxparser.parse(saxparser.java:390) 04-05 15:24:52.707: w/system.err(4784):     @ .details.comptedetails$saxhelper2.parsecontent(comptedetails.java:222) 04-05 15:24:52.707: w/system.err(4784):     @ .details.comptedetails$loadingtask.doinbackground(comptedetails.java:193) 04-05 15:24:52.710: w/system.err(4784):     @ .details.comptedetails$loadingtask.doinbackground(comptedetails.java:1) 04-05 15:24:52.710: w/system.err(4784):     @ android.os.asynctask$2.call(asynctask.java:185) 04-05 15:24:52.710: w/system.err(4784):     @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:306) 04-05 15:24:52.714: w/system.err(4784):     @ java.util.concurrent.futuretask.run(futuretask.java:138) 04-05 15:24:52.714: w/system.err(4784):     @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) 04-05 15:24:52.714: w/system.err(4784):     @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) 04-05 15:24:52.718: w/system.err(4784):     @ java.lang.thread.run(thread.java:1019) 04-05 15:24:52.718: w/system.err(4784):     @ java.net.url.<init>(url.java:273) 04-05 15:24:52.722: w/system.err(4784):     @ java.net.url.<init>(url.java:157) 04-05 15:24:52.722: w/system.err(4784):     @ org.apache.harmony.xml.expatparser.openurl(expatparser.java:753) 

this work expected

type.java

package com.example.test;  public class type {     private string lory;     private string car;      public string getlory()     {         return lory;     }      public void setlory(string lory)     {         this.lory = lory;     }      public string getcar()     {         return car;     }      public void setcar(string car)     {         this.car = car;     }      @override     public string tostring()     {         return "lory : " + this.lory + "\ncar : " + this.car;     }      public string getdetails()     {         string result = "lory : " + this.lory + "\ncar : " + this.car;         return result;     } } 

saxxmlhandler.java

package com.example.test;  import java.util.arraylist; import java.util.list;  import org.xml.sax.attributes; import org.xml.sax.saxexception; import org.xml.sax.helpers.defaulthandler;  public class saxxmlhandler extends defaulthandler {     private list<type> types;     private string tempval;     private type temptype;      public saxxmlhandler()     {         types = new arraylist<type>();     }      public list<type> gettypes()     {         return types;     }      // event handlers     @override     public void startelement(string uri, string localname, string qualifiedname, attributes attributes) throws saxexception     {         // reset         tempval = "";         if ( qualifiedname.equalsignorecase("data") )         {             // create new instance of type             temptype = new type();         }     }      @override     public void characters(char[] ch, int start, int length) throws saxexception     {         tempval = new string(ch, start, length);     }      @override     public void endelement(string uri, string localname, string qualifiedname) throws saxexception     {         if ( qualifiedname.equalsignorecase("type") )         {             // add list , create new instance             types.add(temptype);             temptype = new type();         }         else if ( qualifiedname.equalsignorecase("lory") )         {             temptype.setlory(tempval);         }         else if ( qualifiedname.equalsignorecase("car") )         {             temptype.setcar(tempval);         }     } } 

saxxmlparser.java

package com.example.test;  import java.io.inputstream; import java.util.list;  import javax.xml.parsers.saxparserfactory;  import org.xml.sax.inputsource; import org.xml.sax.xmlreader;  import android.util.log;  public class saxxmlparser {     public static list<type> parse(inputstream is)     {         list<type> types = null;         try         {             // create xmlreader saxparser             xmlreader xmlreader = saxparserfactory.newinstance().newsaxparser().getxmlreader();             // create saxxmlhandler             saxxmlhandler saxhandler = new saxxmlhandler();             // store handler in xmlreader             xmlreader.setcontenthandler(saxhandler);             // process starts             xmlreader.parse(new inputsource(is));             // `type list`             types = saxhandler.gettypes();         }         catch ( exception ex )         {             log.d("xml", "saxxmlparser: parse() failed");         }         // return type list         return types;     } } 

saxparseractivity.java

package com.example.test;  import java.io.bytearrayinputstream; import java.util.list;  import android.app.activity; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.toast;  public class saxparseractivity extends activity implements onclicklistener {     button button;     list<type> types = null;      @override     public void oncreate(bundle savedinstancestate)     {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);         button = (button) findviewbyid(r.id.button);         button.setonclicklistener(this);     }      @override     public void onclick(view v)     {         string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <data> <type> <lory>vroom</lory> <car>crack</car> </type> <type> <lory>doom</lory> <car>chack</car> </type> </data>";         types = saxxmlparser.parse(new bytearrayinputstream(xml.getbytes()));         log.d("ssddsd", "length : " + "" + types.size());         ( type type : types )         {             log.d("saxparseractivity", type.tostring());             toast.maketext(getapplicationcontext(), type.tostring(), toast.length_short).show();         }     } } 

main.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="vertical"     android:padding="10dip" >     <button         android:id="@+id/button"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="parse xml using sax" /> </linearlayout> 

you can see output in both logcat , toast.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -