我尝试使用 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;
}
}
}
}