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 5 of 5

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

    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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: problems with my code

    The method getFirstChild() is undefined for the type Element
    Have you looked at the API doc for the class (Element) you are using to see what the problem is?
    Did you spell it correctly?
    Are you giving it the correct arguments?

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

    Default Re: problems with my code

    I have checked the spelling and everything and it's correct. It shouldn't be a problem cause I got the code en example somewhere. What do you mean with the API doc?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,441
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: problems with my code

    Quote Originally Posted by poppe View Post
    What do you mean with the API doc?
    Meet your new best friend: Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: problems with my code

    For android packages:
    Package Index | Android Developers

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, 03:31 PM
  2. Problems with my code
    By poppe in forum Member Introductions
    Replies: 1
    Last Post: January 26th, 2012, 04:53 AM
  3. Problems with a code
    By oriol in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2011, 07:02 PM
  4. phone book code problems
    By mu'min in forum Member Introductions
    Replies: 1
    Last Post: December 16th, 2010, 08:07 AM
  5. Problems with Code?
    By Sean137 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 14th, 2010, 02:15 PM