user4591082 February 2016

SAX Parser not displaying full XML

I am having trouble with the SAX Parser where it is not retrieving the full xml document. I can see that it originates from the list as only 1 entry is added to the list when there should really be 3 entries.

Can anyone solve the issue by allowing all three entries to be display? Thank You.

catalogue.xml

<?xml version="1.0" ?>
<CATALOG>
  <BOOK>

    <TITLE>A Certain 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 ge        

Answers


wero February 2016

You want to collect information about every BOOK element in your XML document.

Therefore in startElement and endElement you should watch for the start and end of BOOK and 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 it to the list
        myCata.add(tempEmp);
    }
    ...

Consequently you should rename your Catalogue class to Book class since it represents a single book and not a catalogue (= collection of books).

Post Status

Asked in February 2016
Viewed 1,950 times
Voted 10
Answered 1 times

Search




Leave an answer