public class FeedFunctions {
public static Document getFeed(String string) {
Document doc = null;
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// Url to get feed from
URL u = new URL(string);
doc = builder.parse(u.openStream());
NodeList nodes = doc.getElementsByTagName("item");
for(int i=0;i<nodes.getLength();i++) {
Element element = (Element)nodes.item(i);
Log.d(Constants.PREF_NAME, getElementValue(element, "title"));
// FIX!!! Cuts off title if " ' " if there
//String title = getElementValue(element,"title").replace("'", "'");
//Log.d(Constants.PREF_NAME, title);
}
} catch(Exception e) {
Log.d(Constants.PREF_NAME, "Error getting articles!");
Log.d(Constants.PREF_NAME, e.toString());
}
return doc;
}
public final static String getValue(Node elem) {
Node kid;
if( elem != null){
if (elem.hasChildNodes()){
for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
if( kid.getNodeType() == Node.TEXT_NODE ){
return kid.getNodeValue();
}
}
}
}
return null;
}
public static String getElementValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return FeedFunctions.getValue(n.item(0));
}
}