I am trying to retrieve data from an RSS Feed. My program is working well, with one exception. The feed has items that are structured as:
<title></title>
<link></link>
<description></description>
I can retrieve the data, but when the title has a '&' character the string returned stops at the character before. So for example, this title:
<title>A&T To Play Four Against Bears</title>
I am only getting back an 'A', when I expect to get back 'A&T To Play Four Against Bears'.
Can anyone tell me if I can modify my existing RSSReader class to account for the presence of an &:
import android.util.Log;
import java.net.URL; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.CharacterData; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList;
public class RSSReader {
private static RSSReader instance = null;
private RSSReader() {
}
public static RSSReader getInstance() {
if (instance == null) {
instance = new RSSReader();
}
return instance;
}
public ArrayList<Story> getStories(String address) {
ArrayList<Story> stories = new ArrayList<Story>();
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL(address);
Document doc = builder.parse(u.openStream());
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
Story currentStory = new Story(getElementValue(element, "title"),
getElementValue(element, "description"),
getElementValue(element, "link"),
getElementValue(element, "pubDate"));
stories.add(currentStory);
}//for
}//try
catch (Exception ex) {
if (ex instanceof java.net.ConnectException) {
}
}
return stories;
}
private String getCharacterDataFromElement(Element e) {
try {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
} catch (Exception ex) {
Log.i("myTag2", ex.toString());
}
return "";
} //private String getCharacterDataFromElement
protected float getFloat(String value) {
if (value != null && !value.equals("")) {
return Float.parseFloat(value);
} else {
return 0;
}
}
protected String getElementValue(Element parent, String label) {
return getCharacterDataFromElement((Element) parent.getElementsByTagName(label).item(0));
}
}
Any ideas on how to solve this?