0

伙计们以下是我要解析的xml

<?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>

以下是我正在使用的解析器的代码

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() 是给我 expatParser 异常的代码,我在之前的 5 个 xml 解析中一直使用相同的逻辑,但我没有收到此错误。我做错了什么还是xml错了?

4

2 回答 2

0

我认为解析器在到达 & 字符时会抛出异常。您可以在此处找到与您类似的问题

于 2011-04-05T09:15:10.787 回答
0

是的,之前的评论是正确的,解析器在到达 & 或任何特殊字符时抛出异常,有两个选项。

  1. 您需要替换特殊字符(使用编码值)
  2. 您需要将节点与包含特殊字符的 CDATA 绑定..

祝一切顺利

于 2013-12-11T10:23:22.717 回答