Hello!
I'm new here and I'm new att java programming. I have to similar problems with my code.
Here's my entire code:
package tingsholm.app.pwar;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.os.Bundle;
import android.sax.Element;
import android.widget.TextView;
public class Nyhetsflöde extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Nyhetflöde tab");
setContentView(textview);
}
private static Nyhetsflöde instance = null;
private Nyhetsflöde() {
}
public static Nyhetsflöde getInstance() {
if(instance == null)
instance = new Nyhetsflöde();
return instance;
}
public void writeNews() {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBu ilder();
URL u = new URL("http://test.medieprogrammet.eu/#home"); // your feed url
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);
System.out.println("Title: " + getElementValue(element,"title"));
System.out.println("Link: " + getElementValue(element,"link"));
System.out.println("Publish Date: " + getElementValue(element,"pubDate"));
System.out.println("Author: " + getElementValue(element,"dc:creator"));
System.out.println("Description: " + getElementValue(element,"description"));
System.out.println();
}//for
}//try
catch(Exception ex) {
ex.printStackTrace();
}
}
private String getCharacterDataFromElement(Element e) {
try {
Node child = e.getFirstChild();
if(child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
}catch(Exception ex) {
}
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));
}
public static void main(String[] args) {
Nyhetsflöde reader = Nyhetsflöde.getInstance();
reader.writeNews();
}
}
first problem: The method getFirstChild() is undefined for the type Element
second problem: The method getElementsByTagName(String) is undefined for the type Element
the red color shows what's wrong.
help me please!