Android : Cdata in xml not parsing correctly(sax)

夙愿已清 提交于 2019-12-06 13:27:43

The solution to your problem is using a StringBuffer while reading characters and in your endElement use buffer.toString() to get all data. Here are some code snippets:

    private StringBuffer buffer;

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        buffer = new StringBuffer();
        // TODO: your other code
    }

    public void characters(char[] ch,int start, int length) throws SAXException{
        String readChars = new String(ch,start,length);
        if(buffer != null) buffer.append(readChars);
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        currentValue  = buffer.toString();
        // TODO: your other code
    }

Hope it helps.

I will provide following logic for parsing such xml file, which worked for me.

  final ArrayList<NewsItem> newsList=new ArrayList<NewsItem>();
        try
        {
            URL urlexec=new URL("http://www.briefsonline.co.ukname/example.xml");
            URLConnection connection=urlexec.openConnection();
            InputStream input=connection.getInputStream();
            SAXParserFactory factory=SAXParserFactory.newInstance();
            SAXParser parser=factory.newSAXParser();

            parser.parse(input,new DefaultHandler(){

                boolean itemTagStarted=false;
                String currentTag="";           
                StringBuffer buffer;
                NewsItem item=null;
                @Override
                public void startDocument() throws SAXException {
                    Log.v("parsing started!!!","parsing started!!!");

                }
                @Override
                public void startElement(String uri, String localName,String qName, Attributes attributes)
                throws SAXException {
                    currentTag=localName;
                    Log.v("in start elelmet", "in start elelmet");
                    if(qName.equals("article"))
                    {
                        buffer=new StringBuffer();
                        itemTagStarted=true;

                        item=new NewsItem();
                    }

                }
                @Override
                public void characters(char[] ch, int start, int length)
                        throws SAXException {

                    if(itemTagStarted)
                    {
                        if(currentTag.equals("name"))
                        {
                            item.setCatname(String.valueOf(ch,start,length));

                        }
                        if(currentTag.equals("headline"))
                        {

                            item.setCatid(String.valueOf(ch,start,length));
                        }

                        if(currentTag.equals("htmltext"))
                        {
                            buffer.append(String.valueOf(ch,start,length));

                        }
                    }
                }

                @Override
                public void endElement(String uri, String localName,
                        String qName) throws SAXException {
                    currentTag="";

                    if(qName.equals("article"))
                    {
                        itemTagStarted=false;
                        newsList.add(item);

                    }
                    if(qName.equals("htmltext"))
                    {
                        item.setCatdesc(buffer.toString());
                        Log.v("Description data",buffer.toString());

                    }
                }
                @Override
                public void endDocument() throws SAXException {
                    Log.v("parsing completed","parsing completed : array size : "+newsList.size());
                }

            });
        }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }

In this NewsItem is simple java class for different items present in xml like name, headline,httptext, website etc. having setters and getter methods.
Hope it will help you .

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!