Tema: Ayuda Rss
Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/11/2009, 09:31
Avatar de n_n
n_n
 
Fecha de Ingreso: noviembre-2009
Mensajes: 50
Antigüedad: 15 años, 4 meses
Puntos: 1
Exclamación Ayuda Rss

Hola, espero puedan ayudarme con esto...estoy tratando de rescatar los datos de un rss/xml de yahoo, que es el siguiente:

http:
//weather.yahooapis.com/forecastrss?w=2502265

y para ello estoy utilizando este codigo:

Código:
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.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class RSSReader {

    private String titulo;

    private static RSSReader instance = null;

    private RSSReader() {
    }

    public static RSSReader getInstance() {
        if (instance == null) {
            instance = new RSSReader();
        }
        return instance;
    }

    public void writeNews() {
        try {

            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            URL u = new URL("weather.yahooapis.com/forecastrss?w=349871&u=c");

            Document doc = builder.parse(u.openStream());

            NodeList nodosCanal = doc.getElementsByTagName("channel");

            NodeList nodosImg=  doc.getElementsByTagName("image");

            for (int i = 0; i < nodosCanal.getLength(); i++) {

                Element element = (Element) nodosCanal.item(i);

                
               System.out.println("*Title: " + element.getSchemaTypeInfo()+"**************");
                System.out.println("*Link: " + getElementValue(element, "link"));
            }//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);
        }
        return 0;
    }

    protected String getElementValue(Element parent, String label) {
        return getCharacterDataFromElement((Element) parent.getElementsByTagName(label).item(0));
    }

    public String getTitulo() {
        return titulo;
    }

public static void main(String[] args) {
		RSSReader reader = RSSReader.getInstance();
		reader.writeNews();
		}

}
pero mi problema es que no puedo rescatar los valores que son por ejemplo asi:

Código:
 <yweather:location city="Sunnyvale" region="CA" country="United States" />
o asi:

Código:
<yweather:condition text="Fair" code="33" temp="45" date="Tue, 24 Nov 2009 6:56 am PST" />
alguien me puede hechar una manito??

salu2