Why is my SAX handler returning an object with no values? I am setting it just fine

Posted by Blankman on Stack Overflow See other posts from Stack Overflow or by Blankman
Published on 2011-11-28T00:07:18Z Indexed on 2011/11/28 1:50 UTC
Read the original article Hit count: 147

Filed under:
|
|
|

I'm writing a SAX parser for an xml, and the object it returns doesn't have the values that I am setting in the events.

My classes structure is like this:

public class ProductSAXHandler extends DefaultHandler {

  private Product product;

  public ProductSAXHandler() {
     product = new Product();
  }

  public Product ParseXmlFile(String xml) {

     SAXParserFactory spf = new ...
     XMLReader parser = ....


     parser.parse(xml);


     return product;
  }


  public void StartElement(....) {


    for(int ...) { // looping through attributes

      if(qName == "description" && name == "sku") {
        product.setSKU(value);
      }
    }

   }

}

When I am in debug mode, the value of product does get set, and I can see that the product's sku field has the correct value.

But for some reason the product object returned is just a new Product object with no values set during the parsing.

What am I doing wrong here? It must be me not understanding how these events are fired etc.

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml