java - SAX Parser not displaying full XML -


i having trouble sax parser not retrieving full xml document. can see originates list 1 entry added list when there should 3 entries.

can solve issue allowing 3 entries display? thank you.

catalogue.xml

<?xml version="1.0" ?>  <catalog>    <book>        <title>a justice</title>      <author>p.d. james</author>      <year-published>1998</year-published>      <isbn>0375401091</isbn>      </book>    <book>        <title>ashworth hall</title>      <author>anne perry</author>      <year-published>1997</year-published>      <isbn>0449908445</isbn>      </book>    <book>        <title>l.a. confidential</title>      <author>james ellroy</author>      <year-published>1997</year-published>      <isbn>0446674249</isbn>      </book>    <book>        <title>shadow woman</title>      <author>thomas perry</author>      <year-published>1997</year-published>      <isbn>0679453024</isbn>      </book>    </catalog>

catalogue.java

package londonmet.cs.xml;    public class catalogue {      public string title;    public string author;    public string yearpublished;    public string isbn;      public string gettitle() {      return title;    }    public void settitle(string title) {      this.title = title;    }    public string getauthor() {      return author;    }    public void setauthor(string author) {      this.author = author;    }    public string getyearpublished() {      return yearpublished;    }    public void setyearpublished(string yearpublished) {      this.yearpublished = yearpublished;    }    public string getisbn() {      return isbn;    }    public void setisbn(string isbn) {      this.isbn = isbn;    }      @    override    public string tostring() {      return "catalogue::  title=" + this.title + " author=" + this.author + " year published=" + this.yearpublished +        " isbn=" + this.isbn;    }            }

saxparserexample.java

package londonmet.cs.xml;        import java.io.ioexception;  import java.util.arraylist;  import java.util.iterator;  import java.util.list;    import javax.xml.parsers.parserconfigurationexception;  import javax.xml.parsers.saxparser;  import javax.xml.parsers.saxparserfactory;    import org.xml.sax.attributes;  import org.xml.sax.saxexception;    import org.xml.sax.helpers.defaulthandler;    public class saxparserexample extends defaulthandler {      list < catalogue > mycata;      private string tempval;      //to maintain context    private catalogue tempemp;        public saxparserexample() {      mycata = new arraylist < catalogue > ();    }      public void runexample() {      parsedocument();      printdata();    }      private void parsedocument() {        //get factory      saxparserfactory spf = saxparserfactory.newinstance();      try {          //get new instance of parser        saxparser sp = spf.newsaxparser();          //parse file , register class call backs        sp.parse("catalogue.xml", this);        } catch (saxexception se) {        se.printstacktrace();      } catch (parserconfigurationexception pce) {        pce.printstacktrace();      } catch (ioexception ie) {        ie.printstacktrace();      }    }      /**     * iterate through list , print     * contents     */    private void printdata() {        system.out.println("no of books '" + mycata.size() + "'.");        iterator < catalogue > = mycata.iterator();      while (it.hasnext()) {        system.out.println(it.next().tostring());      }    }        //event handlers    public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception {      //reset      tempval = "";      if (qname.equalsignorecase("catalog")) {        //create new instance of employee        tempemp = new catalogue();        }    }        public void characters(char[] ch, int start, int length) throws saxexception {      tempval = new string(ch, start, length);    }      public void endelement(string uri, string localname, string qname) throws saxexception {        if (qname.equalsignorecase("catalog")) {        //add list        mycata.add(tempemp);        } else if (qname.equalsignorecase("title")) {        tempemp.settitle(tempval);      } else if (qname.equalsignorecase("author")) {        tempemp.setauthor(tempval);      } else if (qname.equalsignorecase("year-published")) {        tempemp.setyearpublished(tempval);      } else if (qname.equalsignorecase("isbn")) {        tempemp.setisbn(tempval);      }      }      public static void main(string[] args) {      saxparserexample spe = new saxparserexample();      spe.runexample();    }    }

you want collect information every book element in xml document.

therefore in startelement , endelement should watch start , end of book , not catalog elements:

public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception {     // reset     tempval = "";     if (qname.equalsignorecase("book")) // not catalog         ... }   public void endelement(string uri, string localname, string qname) throws saxexception {     if (qname.equalsignorecase("book"))  // not catalog     {         // add list         mycata.add(tempemp);     }     ... 

consequently should rename catalogue class book class since represents single book , not catalogue (= collection of books).


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -