xml ile haber verisi çekme

  • Konbuyu başlatan hengame
  • Başlangıç tarihi
H
hengame
Misafir
PHP:
/*
* Main.java
*
* Created on 03 October 2006, 13:28
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication2;
import com.sun.org.apache.bcel.internal.generic.GOTO;
import java.lang.*;    import java.io.*;
import java.awt.*;    import java.awt.event.*;
import java.net.*;
import java.net.URL;
import javax.swing.*;    import javax.swing.event.*;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;


    
    /** Creates a new instance of Main */
public class Main{
    


public static void main (String argv []){

        Gui gui = new Gui();
        gui.getnews(gui.chc.getSelectedItem());
        
   }

}
class Gui extends JFrame
{
    JFrame fr = new JFrame("Haber Servisi");
    TextArea txt_hbr =new TextArea();
    Panel pnl = new Panel(new FlowLayout(FlowLayout.RIGHT));
    Choice chc = new Choice();
    Button bt_get=new Button("Getir");
    Gui()
    {
        fr.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
        fr.add(txt_hbr);
        chc.add("Hepsi");
        chc.add("Yaşam");
chc.add("Türkiye");
chc.add("Politika");
        chc.add("Dış Haberler");
        chc.add("Ekonomi");
        chc.add("Spor");
        chc.add("Kültür/Sanat");
        pnl.add(chc,BorderLayout.EAST);
        bt_get.addActionListener((new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            getnews(chc.getSelectedItem());
            }
        }));
        pnl.add(bt_get);
        Button bt_close=new Button("Kapat");
        pnl.add(bt_close);
        bt_close.addActionListener(new ActionListener()
        {  public void actionPerformed(ActionEvent e)
            { System.exit(0); } } );
        
        fr.add(pnl,BorderLayout.SOUTH);
        fr.setSize(455,250);
        fr.setVisible(true);
    }
    public void getnews(String konu)
    {  
        txt_hbr.setText("");
         try
         {
            
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                Document doc = connect(docBuilder,"http://www.radikal.com.tr/radikal.xml");

                // normalize text representation
                doc.getDocumentElement ().normalize ();
               // gui.tb_hbr.setText ("Root element of the doc is " + doc.getDocumentElement().getNodeName());


                NodeList listofhbr = doc.getElementsByTagName("item");
                int totalhbr = listofhbr.getLength();
             //   txt_hbr.setText ("Toplam Haber : " + totalhbr);

                for(int s=0; s<listofhbr.getLength() ; s++)
                {

                    Node firstPersonNode = listofhbr.item(s);
                    if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


                    Element firstPersonElement = (Element)firstPersonNode;
                    //-------
                    NodeList firstNameList = firstPersonElement.getElementsByTagName("category");
                    Element firstNameElement = (Element)firstNameList.item(0);
                    NodeList textFNList = firstNameElement.getChildNodes();
               //     txt_hbr.setText(((Node)textFNList.item(0)).getNodeValue().trim().toString());
                    if (konu.equals("Hepsi"))
                    {
                        txt_hbr.setText(txt_hbr.getText()+"\nKategori : " +((Node)textFNList.item(0)).getNodeValue().trim());
                        //-------
                        NodeList lastNameList = firstPersonElement.getElementsByTagName("title");
                        Element lastNameElement = (Element)lastNameList.item(0);
                        NodeList textLNList = lastNameElement.getChildNodes();
                        txt_hbr.setText (txt_hbr.getText()+"\nBaslik :"+ ((Node)textLNList.item(0)).getNodeValue().trim());

                        //----
                        NodeList ageList = firstPersonElement.getElementsByTagName("description");
                        Element ageElement = (Element)ageList.item(0);

                        NodeList textAgeList = ageElement.getChildNodes();
                        txt_hbr.setText (txt_hbr.getText()+"\n>> :" + ((Node)textAgeList.item(0)).getNodeValue().trim());
                    }
                    if(((Node)textFNList.item(0)).getNodeValue().trim().toString().equals(konu))
                    {
                        txt_hbr.setText(txt_hbr.getText()+"\nKategori : " +((Node)textFNList.item(0)).getNodeValue().trim());
                        //-------
                        NodeList lastNameList = firstPersonElement.getElementsByTagName("title");
                        Element lastNameElement = (Element)lastNameList.item(0);
                        NodeList textLNList = lastNameElement.getChildNodes();
                        txt_hbr.setText (txt_hbr.getText()+"\nBaslik :"+ ((Node)textLNList.item(0)).getNodeValue().trim());

                        //----
                        NodeList ageList = firstPersonElement.getElementsByTagName("description");
                        Element ageElement = (Element)ageList.item(0);

                        NodeList textAgeList = ageElement.getChildNodes();
                        txt_hbr.setText (txt_hbr.getText()+"\n>> :" + ((Node)textAgeList.item(0)).getNodeValue().trim());
                    }
                    //------


                    }//end of if clause


                }//end of for loop with s var


                }catch (Throwable t) {
                t.printStackTrace ();
                }

    }  
    public Document connect(DocumentBuilder db, String urlString)
    {
          
       // try {
          
           //yeni url olusturuluyor
          
           try {
               URL url = new URL( "http://www.radikal.com.tr/radikal.xml ");
               URLConnection URLconnection =url.openConnection () ;
               //HttpURLConnection httpConnection =(HttpURLConnection)URLconnection;
               //HttpURLConnection httpConnection= (HttpURLConnection).URLconnection.connect();
               // HttpURLConnection httpConnection = HttpURLConnection.URLconnection);
               //HttpURLConnection httpConnection = (HttpURLConnection) url.getConnection();
              // HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
                HttpURLConnection httpConnection =(HttpURLConnection)URLconnection;
               //gelen cevaba bak?l?yor
               int responseCode =httpConnection.getResponseCode ( ) ;
               //baglant? kuruldyusa i?leme ba?lan?yor
               if ( responseCode == HttpURLConnection.HTTP_OK) {
                   InputStream in = httpConnection.getInputStream ( ) ;
                   try {
                       //gelen stream XML dokuman?na cevriliyor
                       Document doc = db.parse( in );
                       return doc;
                   }
                   catch(org.xml.sax.SAXException e){
                       System.out.println("hata");
                       e.printStackTrace ( ) ;
                   }
               }
               else{
                   System.out.println( "HTTP connection response != HTTP_OK" );
               }
               httpConnection.disconnect();
           }
           catch ( IOException e ) {
               System.out.println("hata");
               e.printStackTrace ( ) ;
           }
           return lisanssız;
       }
      /* catch ( MalformedURLException e ) {  
           System.out.println("hata");
           e.printStackTrace ( ) ;
       }*/
      
   }

Dokümanı Değerlendirin. DEĞERLENDİR:

arkadaslar bir kod buldum xml den veri çekmek için kullanan arkadaslar da varmıs nsıl kullanabilirim bunu js olarak kaydetim

index e de
PHP:
<script type="text/javascript" src="haber.js"></script>



bunu attın ççalışmadı yardımlarınızı bekliorum help me
 
0
Onaylı Üye
Üye
Katılım
16 Kasım 2005
Mesajlar
2,778
Tepki puanı
0
Konum
Ankara/Kayseri
php kod diye verdiğiniz java kodu.
Bunun dışında birçok eksik var.
Misal 1. satırdaki package javaapplication2; Bu import edilmemiş
 
0
Onaylı Üye
Üye
Katılım
5 Temmuz 2004
Mesajlar
1,920
Tepki puanı
35
böyle birşeye benimde ihtiyacım var buldugunda paylaşırsan sevinirim
iyi çalışmalar
 
0
Onaylı Üye
Üye
Katılım
11 Eylül 2006
Mesajlar
410
Tepki puanı
1
Joomla kullananlar site modullerine girip yeni modül açıp rss kısmına haber sitelerinden alacakları xml yada rss adresini kopyalayıp altta kaç haber göstereceği ve haberin geldiği sitenin isminin ve açıklamasının çıkıp çıkmayacağını işaretliyip kaydederek yapabilir.
 
0
Onaylı Üye
Üye
Katılım
2 Şubat 2006
Mesajlar
1,410
Tepki puanı
4
Konum
Pendik
banada böyle birşey LAZIM :)
 
0
Onaylı Üye
Üye
Katılım
10 Kasım 2003
Mesajlar
0
Tepki puanı
3
Konum
istanbul
Bu kodlar java script değil Java Applet kodları derlenmeden çalışmaz.
 
0
Onaylı Üye
Üye
Katılım
2 Şubat 2006
Mesajlar
1,410
Tepki puanı
4
Konum
Pendik
Arkadaşlar şöyle bir script varmı , haber sitesindeki haberi çekip kendi sitemde yayınlamak istiyorum çekilen haberleri db yazıcak
 
Geri
Üst