Java Sax Parser只返回一行标记

时间:2013-03-31 20:55:08

标签: java xml rss saxparser rss-reader

我正在尝试解析xml中的description标记,但它只输出一行:

description: <img src=http://www.ovations365.com/sites/ovations365.com/images/event/441705771/sparkswebsite_medium.jpg alt="SPARKS: Understanding Energy">

这只是CDATA中文本的一小部分,我正在尝试输出多个项目的描述。为什么我不能获得整个CDATA?

XML位于:http://feeds.feedburner.com/Events-Ovations365

package com.example.ovations_proj;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.example.ovations_proj.RssItem;


public class RssParseHandler extends DefaultHandler {

    private List<RssItem> rssItems;

    // Used to reference item while parsing
    private RssItem currentItem;

    // Parsing title indicator
    private boolean parsingTitle; 
    // Parsing link indicator
    private boolean parsingLink; 
    private boolean parsingDes;


    public RssParseHandler() {
        rssItems = new ArrayList<RssItem>();
    }

    public List<RssItem> getItems() {
        return rssItems;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        System.out.println("Start Element :" + qName);
        if ("item".equals(qName)) { //item
            currentItem = new RssItem();
        } else if ("title".equals(qName)) { //title
            parsingTitle = true;
        } else if ("link".equals(qName)) { //link
            parsingLink = true;
        }else if ("description".equals(qName) ) { //description
            parsingDes = true;
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        System.out.println("End Element :" + qName);
        if ("item".equals(qName)) {
            rssItems.add(currentItem);//item
            currentItem = null;         
        } else if ("title".equals(qName)) {//title
            parsingTitle = false;
        } else if ("link".equals(qName)) {//link
            parsingLink = false;
        } else if ("description".equals(qName) ) {  //description

            parsingDes = false;         
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (parsingTitle) {
            if (currentItem != null){
                currentItem.setTitle(new String(ch, start, length));                
            }
        } else if (parsingLink) { 
            if (currentItem != null) {
                currentItem.setLink(new String(ch, start, length));
                parsingLink = false;
            }
        } else if (parsingDes) {       
            if (currentItem != null) {                      
                currentItem.setDes(new String(ch, start, length));
                System.out.println("description:  "  + currentItem.getDes());
                parsingDes = false;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

似乎<![CDATA[...]]>部分中的字符数据是以多个块发送的,即多次调用characters方法。

ContentHandler documentation for the characters method提到SAX解析器可以自由地执行此操作:

  

SAX解析器可以在一个块中返回所有连续的字符数据,或者它们可以将它分成几个块[....]

因此,您需要调整characters方法,以便为同一块连续字符数据多次调用。