Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problems with my code

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with my code

    Hi!

    I have to problems with this 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();
    }
    }

    Problem number one: The method getFirstChild() is undefined for the type Element
    Problem number two: The method getElementsByTagName(String) is undefined for the type Element

    I have marked the errors in red color.
    I would be very pleased if I get an as easy answer as possible because I'm new at this.

    Thanks!


  2. #2
    Member OA-OD-JD1's Avatar
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    69
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post
    Blog Entries
    29

    Default Re: Problems with my code

    In future, please use the Java highlighting code.

    [highlight=java][/highlight]

    Be sure to post in the right section also.

Similar Threads

  1. [SOLVED] n00b having problems running code
    By cha0s619 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 10th, 2012, 04:31 PM
  2. Problems with a code
    By oriol in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2011, 08:02 PM
  3. phone book code problems
    By mu'min in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 16th, 2010, 11:36 AM
  4. phone book code problems
    By mu'min in forum Member Introductions
    Replies: 1
    Last Post: December 16th, 2010, 09:07 AM
  5. Problems with Code?
    By Sean137 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 14th, 2010, 03:15 PM

Tags for this Thread