1

我尝试使用 StringBuilder 命名对象,但我仍然没有从描述标签中获取所有 CDATA。xml 位于Events-Ovations365:

基本上它只在一行上获取 CDATA:

img 是:http ://www.ovations365.com/sites/ovations365.com/images/org/81/newtown_medium.jpg

alt="Ocmulgee Heritage Trail 剪彩">

package com.example.ovations_proj;

import java.io.ByteArrayInputStream;
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 android.util.Log;

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;

    StringBuilder obj;


    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 ("description".equals(qName) ) { //description
            obj = new StringBuilder();
            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 ("description".equals(qName)) {   //description           
            String theFullText = obj.toString();
            System.out.println("fulltext data:  "  + theFullText);
            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 (parsingDes) {       
            if (currentItem != null && obj!=null ) {                                
                obj.append(ch, start, length);  
                parsingDes = false;
            }
        }
    }
}
4

2 回答 2

3

It's probably because you're not implementing the characters method correctly, see Oracle's tutorial:

Parsers are not required to return any particular number of characters at one time. A parser can return anything from a single character at a time up to several thousand and still be a standard-conforming implementation. So if your application needs to process the characters it sees, it is wise to have the characters() method accumulate the characters in a java.lang.StringBuffer and operate on them only when you are sure that all of them have been found.

Your code is assuming you're getting the entire text for an element in one call, but that's not guaranteed. The characters method needs to accumulate the text found into a StringBuffer (or StringBuilder or other data structure), but the decisions on what to do with the accumulated text need to be somewhere else, such as in the endElement method. It looks like you are setting a flag prematurely in the characters method and causing the rest of the text to be lost.

于 2013-04-02T12:17:01.190 回答
0

这是对我有用的代码,因为它被注释掉了。我的 parsingDes 标志只导致了一个追加。

@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){// && obj!=null ) {
            obj.append(ch, start, length);
            //parsingDes = false;
        }
    }
于 2013-04-02T20:27:53.733 回答