SAX parser ignoring CDATA - html tags
I have a simple Android RSS reader app in which I am using SAX parser to fetch the data. All the records are being fetched correctly except for the "desc" element. The XML structure is as below.
<item>
<title>Boilermaker Jazz Band</title>
<link>http://eventur.sis.pitt.edu/event.jsp?e_id=1805</link>
<type>Music Concerts</type>
<s_tim开发者_运维百科e>09-02-2010 05:00 PM </s_time>
<venue>Backstage Bar at Theater Square</venue>
<venue_addr/>
<desc>
<p><span style="font-family: arial, geneva, sans-serif; font-size: 11px;">
<p style="font-family: Arial, Helvetica, sans-serif; max-width: 600px; margin-top: 8px; margin-right: 0px; margin-bottom: 8px; margin-left: 0px; font-size: 9pt; vertical-align: top;">Authentic American Jazz, Ragtime and Swing The Boilermaker Jazz Band is an ecstatically fun band performing authentic hot jazz, ragtime, and swing. The group has ....</desc>
−
<img_link>
http://eventur.sis.pitt.edu/images/Boilheadshot1.jpg
</img_link>
</item>
Data from all field is fetched as a whole. But when it comes to <desc>
, the 'characters' method just fetches "<" and ignores the rest. Could some one please advise what could be done.
Your <desc>
element contains another (invalid) XML structure. In your example, startElement()
will be triggered for <p>
, then <span>
, then another <p>
. If you want to extract only text, you could concatenate what the characters()
method returns for all children of <desc>
until you get notified of the end of <desc>
element with endElement()
.
Something like
private boolean isDescStarted = false;
private StringBuilder textDesc = new StringBuilder();
public void startElement(String uri, String name, String qName, Attributes atts) {
if(name.equals("desc") {isDescStarted = true;}
}
public void endElement(String uri, String name, String qName) {
if(name.equals("desc") {
isDescStarted = false;
String fullTextDesc = textDesc.toString(); // do whatever you want with this string now
}
}
public void characters(char[] buf, int offset, int length) {
if (isDescStarted) {
textDesc.append(new String(buf, offset, length));
}
}
精彩评论