SAX Parser不显示完整的XML

时间:2016-02-08 18:10:04

标签: java xml sax

我在使用SAX Parser时遇到问题,因为它没有检索完整的xml文档。我可以看到它来自列表,因为当真正有3个条目时,只有1个条目被添加到列表中。

任何人都可以通过允许显示所有三个条目来解决问题吗?谢谢。

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 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 a factory
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {

      //get a new instance of parser
      SAXParser sp = spf.newSAXParser();

      //parse the file and also register this class for call backs
      sp.parse("catalogue.xml", this);

    } catch (SAXException se) {
      se.printStackTrace();
    } catch (ParserConfigurationException pce) {
      pce.printStackTrace();
    } catch (IOException ie) {
      ie.printStackTrace();
    }
  }

  /**
   * Iterate through the list and print
   * the contents
   */
  private void printData() {

    System.out.println("No of Books '" + myCata.size() + "'.");

    Iterator < Catalogue > it = 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 a 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 it to the 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();
  }

}

1 个答案:

答案 0 :(得分:3)

您希望收集有关XML文档中每个BOOK元素的信息。

因此,在startElementendElement中,您应该关注BOOK而不是CATALOG元素的开头和结尾:

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);
    }
    ...

因此,您应该将Catalogue课程重命名为Book课程,因为它代表一本书而不是目录(=书籍集合)。