0

私はいくつかの RSS フィードを解析し (別のもので試しました...)、かなりランダムに文字が切り取られるたびに。私は何を間違っていますか?場合によっては機能し、他の場合は機能しないのはなぜですか? それを行う別の方法はありますか?XML には (ほとんどの場合) UTF-8 文字 (ä、ö、ü など) が含まれるため、ソリューションはそれらの文字でも機能するはずです。

さらに情報が必要な場合 (より多くのコード、より詳細な情報など) は、私に知らせてください!

ここに私のコードがあります:

public class RSSHandler extends DefaultHandler {

final int state_unknown = 0;
final int state_title = 1;
final int state_description = 2;
final int state_link = 3;
final int state_pubdate = 4;
int currentState = state_unknown;
StringBuilder strCharacters;

RSSFeed feed;
RSSItem item;

boolean inEntity = false;
String entityName = "";

boolean itemFound = false;

public RSSHandler() {
    strCharacters = new StringBuilder();
}

public RSSFeed getFeed() {
    return feed;
}

@Override
public void startDocument() throws SAXException {
    feed = new RSSFeed();
    item = new RSSItem();
}

@Override
public void endDocument() throws SAXException {
}

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    strCharacters = new StringBuilder();
    if (localName.equalsIgnoreCase("item")) {
        itemFound = true;
        item = new RSSItem();
        currentState = state_unknown;
    } else if (localName.equalsIgnoreCase("title")) {
        currentState = state_title;
    } else if (localName.equalsIgnoreCase("description")) {
        currentState = state_description;
    } else if (localName.equalsIgnoreCase("link")) {
        currentState = state_link;
    } else if (localName.equalsIgnoreCase("pubdate")) {
        currentState = state_pubdate;
    } else {
        currentState = state_unknown;
    }

}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if (itemFound == true) {
        switch (currentState) {
            case state_title:
                item.setTitle(strCharacters.toString());
                break;
            case state_description:
                break;
            case state_link:
                item.setLink(strCharacters.toString());
                break;
            case state_pubdate:
                String dateStr = strCharacters.toString();
                SimpleDateFormat curFormater = new SimpleDateFormat(
                        "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
                Date dateObj = null;
                try {
                    dateObj = curFormater.parse(dateStr);
                    SimpleDateFormat postFormater = new SimpleDateFormat(
                            "dd.MM.yyyy HH:mm");
                    String newDateStr = postFormater.format(dateObj);
                    item.setPubdate(newDateStr);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    } else {
        switch (currentState) {
            case state_title:
                feed.setTitle(strCharacters.toString());
                break;
            case state_description:
                break;
            case state_link:
                feed.setLink(strCharacters.toString());
                break;
            case state_pubdate:
                feed.setPubdate(strCharacters.toString());
                break;
            default:
                break;
        }
    }

    currentState = state_unknown;

    if (localName.equalsIgnoreCase("item")) {
        feed.addItem(item);
    }
}

public void startEntity(String name) throws SAXException {
    inEntity = true;
    entityName = name;
}

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

    strCharacters = new StringBuilder();
    if (inEntity) {
        inEntity = false;
        strCharacters.append("&" + entityName + ";");
    } else {
        for (int i = start; i < start + length; i++) {
            strCharacters.append(ch[i]);
        }
    }

    // strCharacters.append(ch, start, length);
}

}

4

1 に答える 1

0

StringBuilder呼び出しごとに新しいを作成していcharacters()ます。これは正しくありません。要素ごとに複数の呼び出しが行われcharacters()ます。最後のピースを収集するだけでなく、これらすべての結果を連結する必要があります。

于 2012-06-04T10:53:34.550 に答える