Friday, October 11, 2013

Reading Weather data from xml

      
 Reading Weather data from xml
    try {

                String url = "http://weather.yahooapis.com/forecastrss?w=2442047&u=f";
        System.out.println("::::Weather URL::::"+url);
              
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser saxParser = factory.newSAXParser();

                DefaultHandler handler = new DefaultHandler() {
                    boolean flag;
                    public void startElement(String uri, String localName,
                                             String qName,
                                             org.xml.sax.Attributes attributes) throws SAXException {
                      
                        try {
                            // _logger.info("Start Element :" + qName);
                           
                            if (qName.equalsIgnoreCase("latitude")) {
                                flag=true;
                            }
                            if (qName.equalsIgnoreCase("description")) {
                                System.out.println(":::::::Start::::::Weather Condition:::::::::");
                                flag=true;
                            }
                            if (qName.equalsIgnoreCase("yweather:condition")) {
                                System.out.println(":::::::Start::::::Weather Condition:::::::::");
                                System.out.println(":::text:"+attributes.getValue("text"));
                                System.out.println(":::temp:"+attributes.getValue("temp"));
                                System.out.println(":::date:"+attributes.getValue("date"));
                                System.out.println(":::::::End::::::Weather Condition:::::::::");
                            }

                            if (qName.equals("yweather:forecast")) {
                                System.out.println(":::::::Start::::::Weather Forecast:::::::::");
                                System.out.println("Code:" + attributes.getValue("day"));
                                System.out.println(":::date:"+attributes.getValue("date"));
                                System.out.println(":::low:"+attributes.getValue("low"));
                                System.out.println(":::high:"+attributes.getValue("high"));
                                System.out.println(":::text:"+attributes.getValue("text"));
                                System.out.println(":::::::End::::::Weather Forecast:::::::::");
                            }
                           
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        System.out.println("::::::END Processes::::::;:::");
                    }


                    //                        public void endElement(String uri, String localName,
                    //                                        String qName) throws SAXException {
                    //                        }

                    public void characters(char[] ch, int start,
                                           int length) throws SAXException {
                    if(flag){
                        String s=new String(ch, start, length);
                           
                        System.out.println(":::new String(ch, start, length):::"+s.split("<br />")[0]);
                       
                    flag=false;
                    }
                    }

                };
                System.out.println("::::::::::::::::::::::::::::::::::::::::::::");

                saxParser.parse(url, handler);
                System.out.println(":::::::::::::::::::::::::::");
            } catch (Exception e) {
                e.printStackTrace();
            }


Adding Images to jspx page
<f:verbatim>
                <input type="image"
                       src="http://l.yimg.com/a/i/us/we/52/33.gif"/>
              </f:verbatim>
              <af:image source="http://l.yimg.com/a/i/us/we/52/26.gif" id="i1"/>
              <![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/33.gif"/>]]>
            </af:group>
           
         
      

No comments:

Post a Comment