无法解析数据,因为我收到 expatParser 异常

发布于 2024-10-30 12:48:30 字数 2911 浏览 0 评论 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>

以下是我正在使用 sp.parse() 的解析器的代码

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"));*/
                }
             }

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逐鹿 2024-11-06 12:48:30

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

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

缺⑴份安定 2024-11-06 12:48:30

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

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

一切顺利

yes previous comment is right, parser is throwing the exception when it reaches & or any special character, two options are there.

  1. you need to replace the special character(use encoded value)
  2. you need to bind the node with CDATA which contains special character..

all the best

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文