Search code examples
androidxml-parsingsaxparserexpat-parser

unable to parse data as i am getting an expatParser Exception


guys Following is the xml which i am trying to parse

<?xml version="1.0" encoding="UTF-8"?><Categories><category name="Banquet & Marriage Hall" id="1" image=""/><category name="Crematorium, Burial Ground" id="2" image=""/><category name="Educational Institution" id="3" image=""/><category name="Embassies & Consulates" id="4" image=""/><category name="Fire Station" id="5" image=""/><category name="Government Office" id="6" image=""/></Categories>

Following is the code of my parser which i am using

public byte parse(){

                SAXParserFactory spf = null;
                SAXParser sp = null;
                InputStream inputStream = null;

                try {
                    inputStream = new ByteArrayInputStream(data.getBytes());
                    spf = SAXParserFactory.newInstance();
                    if (spf != null) {
                        sp = spf.newSAXParser();
                        **sp.parse(inputStream, this);**


                    }
                }
                /*
                 * Exceptions need to be handled MalformedURLException
                 * ParserConfigurationException IOException SAXException
                 */

                catch (Exception e) {
                    System.out.println("Exception: " + e);
                    e.printStackTrace();
                } finally {
                    try {
                        if (inputStream != null)
                            inputStream.close();
                    } catch (Exception e) {
                    }
                }

                if (categorieslist != null && categorieslist.size() > 0) {
                //  Log.d("Array List Size",""+tipsList.get(4).getTitle());


                    return 1;
                } else {
                    return 0;
                }

            }

         public ArrayList<Categories> getParserCategoriesList(){
             return categorieslist;
         }

         public void startElement(String uri, String localName, String qName,
                    Attributes attributes) throws SAXException {

             if(localName.equalsIgnoreCase("Categories")){
                if(localName.equalsIgnoreCase("category")){
                    categories = new Categories();
                    categorieslist.add(categories);

                    categories.setId(attributes.getValue("id"));
                    Log.d("ID",attributes.getValue("id"));
                    categories.setName(attributes.getValue("name"));
                    Log.d("NAME",attributes.getValue("name"));
                    /*categories.setImage(attributes.getValue("image"));
                    Log.d("image",attributes.getValue("image"));*/
                }
             }

sp.parse() is the code which is giving me the expatParser Exception I have been using the same logic in previous 5 xml parsing and i dont get this error. What am i doing wrong or is it dat the xml is wrong ??


Solution

  • I think the parser is throwing the exception when it reaches the ampersand (&) character. You can find a problem similar to yours here