facebook

SAX Parser Global Variable problem

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #248837 Reply

    sienaluke
    Member

    Can someone tell me what i am doing wrong.

    i have a javabean called querybean that i store cdata from an xml file. i also have a map that i want to store this querybean in.

    when i debug this, i can see the map being populated in my end element event call, but when i return the map, there is nothing in it.

    I’m not really sure why this is. is the calling of the parser events run on a differnt thread or something.

    here is my code

    public class XmlParsing extends DefaultHandler implements LexicalHandler{

    String reportId = null;
    boolean cdataSection = false;
    StringBuffer cdataString = new StringBuffer();
    Map queryMap = new HashMap();
    QueryBean queryBean = new QueryBean();

    public static final String LEXICAL_SAX_PROPERTY =
    http://xml.org/sax/properties/lexical-handler”;

    public Map XmlPassThrough(File file)
    {

    DefaultHandler handler = new XmlParsing();
    SAXParserFactory factory = SAXParserFactory.newInstance();
    try
    {
    SAXParser saxParser = factory.newSAXParser();

    saxParser.setProperty(“http://xml.org/sax/properties/lexical-handler”,handler);
    saxParser.parse(file,handler);

    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    return this.queryMap;

    }

    /////////////////////////////////////////////////////////
    //////////Setters and Getters////////////////////////////
    ////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////

    public String getReportId() {
    return reportId;
    }

    public void setReportId(String reportId) {
    this.reportId = reportId;
    }

    public boolean isCdataSection() {
    return cdataSection;
    }

    public void setCdataSection(boolean cdataSection) {
    this.cdataSection = cdataSection;
    }

    public StringBuffer getCdataString() {
    return cdataString;
    }

    public void setCdataString(StringBuffer cdataString) {
    this.cdataString = cdataString;
    }

    public Map getQueryMap() {
    return queryMap;
    }

    public void setQueryMap(Map queryMap) {
    this.queryMap = queryMap;
    }

    /* Handler Methods*/
    public void startDocument() throws SAXException{

    System.out.println(“Start of Document”);

    }

    public void endDocument() throws SAXException{

    System.out.println(“End of Document”);

    }

    public void startElement(String namespaceURI,
    String lName,
    String qName,
    Attributes attrs)
    throws SAXException
    {
    String eName = lName;
    if(“”.equals(eName)) eName = qName;

    if(qName.equals(“sql”))
    {

    queryBean.setReportID(attrs.getValue(“report_id”));
    queryBean.setReportType(attrs.getValue(“report_type”));
    if(attrs.getValue(“report_start_date”).equals(“true”))
    {
    queryBean.setReport_start_date(true);
    }
    else
    {
    queryBean.setReport_start_date(false);
    }

    }

    }

    public void endElement(String namespaceURI,
    String sName, // simple name
    String qName // qualified name
    )
    throws SAXException
    {

    if(qName.equals(“select”))
    {
    queryBean.setSelect(this.getCdataString());

    }
    else if(qName.equals(“from”))
    {
    queryBean.setFrom(this.getCdataString());
    }
    else if(qName.equals(“where”))
    {
    queryBean.setWhere(this.getCdataString());
    }
    else if(qName.equals(“group”))
    {
    queryBean.setGroup(this.getCdataString());
    }
    else if(qName.equals(“order”))
    {
    queryBean.setOrder(this.getCdataString());
    }
    else if(qName.equals(“sql”))
    {
    this.queryMap.put(queryBean.getReportID(),queryBean);
    this.setQueryMap(queryMap);
    }

    this.cdataString.setLength(0);

    }

    public void characters(char buf[], int offset, int len)
    throws SAXException
    {

    if(this.isCdataSection())
    {
    String str = new String(buf,offset,len);
    this.cdataString.append(str);

    }

    }

    /* LexicalHandler Methods*/
    public void endCDATA() throws SAXException {
    // TODO Auto-generated method stub
    System.out.println(this.cdataString.toString());
    System.out.println(“end of CDATASECTION”);

    this.setCdataSection(false);

    }

    public void endDTD() throws SAXException {
    // TODO Auto-generated method stub

    }

    public void startCDATA() throws SAXException {
    // TODO Auto-generated method stub
    System.out.println(“Start of CDATASECTION”);
    this.setCdataSection(true);
    }

    public void comment(char[] ch, int start, int length) throws SAXException {
    // TODO Auto-generated method stub

    }

    public void endEntity(String name) throws SAXException {
    // TODO Auto-generated method stub

    }

    public void startEntity(String name) throws SAXException {
    // TODO Auto-generated method stub

    }

    public void startDTD(String name, String publicId, String systemId) throws SAXException {
    // TODO Auto-generated method stub

    } 😕 8) 😕

    #248901 Reply

    Riyad Kalla
    Member

    While you are debugging and see elements in your map, note the ID of the object, then when you step to the part where it is empty, compare the object ID, is it the same? If not then something is creating a new map along the way and returning it instead.

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: SAX Parser Global Variable problem

You must be logged in to post in the forum log in