アットウィキロゴ

RSS02

import java.io.*;
import java.net.*;  
import java.sql.*;

class game0501{

String db52;
String url = "jdbc:derby:db;create=true";  
String usr = "";  
String pwd = "";
String newdata;
String bun;
String z;
int n;
String article[]=new String[100];
int num,s;
String title[]=new String[100];
String des[]=new String[100];
String link[]=new String[100];
String date[]=new String[100];
String author[]=new String[100];
String pub[]=new String[100];
String sub[]=new String[100];
String pubdate[]=new String[100];

public static void main(String [] args) {
        game0501 f=new game0501();        
    }

game0501(){

String urlx;

urlx="http://saayaworld.blog70.fc2.com/?xml";

getdata(urlx);

makearticle();

makeitem();

for (s=1;s<num+1;s++){
des[s]=rev(des[s]);
}


s=1;
for (s=1;s<num+1;s++){
newdata="'"+ title[s] + "',";
newdata=newdata+"'"+ author[s] + "',";
newdata=newdata+"'" + date[s] +"',";
newdata=newdata+"'"+ pub[s] + "',";
newdata=newdata+"'"+ des[s] + "',";
newdata=newdata+"'"+ link[s] + "'";
insertdata(newdata);
}




}

void insertdata(String newdata){;

Connection dbx;
Statement  stx;
String sqx;

sqx="insert into db52 values(";
sqx=sqx+newdata;
sqx=sqx+")";
try {
dbx = DriverManager.getConnection(url, usr, pwd);
stx = dbx.createStatement();
stx.executeUpdate(sqx);
stx.close();
dbx.close();
} catch (Exception ex) {ex.printStackTrace();}

}


void maketable(){

Connection dbx;  
Statement stx;  
String sqx;

sqx="create table db52(";
sqx=sqx+"title varchar(100),";
sqx=sqx+"author varchar(100),";
sqx=sqx+"date varchar(100),";
sqx=sqx+"pub varchar(100),";
sqx=sqx+"des varchar(100),";
sqx=sqx+"link varchar(200))";
try {  
dbx = DriverManager.getConnection(url, usr, pwd);
stx = dbx.createStatement();  
stx.executeUpdate(sqx);

stx.close();
dbx.close();

}
catch (Exception ex) { ex.printStackTrace();}
}

String rev(String str){
String z;
int sx,nx;
int pop;
z=str;
pop=str.indexOf("&lt");
while(pop>-1){
String[] x=str.split("&lt");
z="";
for (sx=0;sx<x.length;sx++){
String[] y=x[sx].split("&gt;");
for (nx=1;nx<y.length;nx++){
z=z+y[1];
}
}
pop=-100;
}
return z;
}


String makedata(String doc,String you){
String sx1,sx2,sx;
int x1,x2;
int lp1;
int lp2;
sx1="<"+you+">";
sx2="</"+you+">";
lp1=sx1.length();
lp2=sx2.length();
x1=doc.indexOf(sx1);
x2=doc.indexOf(sx2);
sx="";
if(x1>-1)sx=doc.substring(x1+lp1,x2);
return sx;
}


void makeitem(){

for (s=1;s<num+1;s++){
title[s]=makedata(article[s],"title");
des[s]=makedata(article[s],"description");
link[s]=makedata(article[s],"link");
date[s]=makedata(article[s],"dc:date");
author[s]=makedata(article[s],"dc:creator");
pub[s]=makedata(article[s],"dc:publisher");
sub[s]=makedata(article[s],"dc:subject");
pubdate[s]=makedata(article[s],"dc:Pubdate");
}

}


void makearticle(){
int point,x,n;
int x2,lp2;
String sx;
point=0;
x=bun.indexOf("<item");
n=0;
while(x>0){
n=n+1;
article[n]=makeart(bun,point);
sx="</item>";
lp2=sx.length();
x2=bun.indexOf(sx,point);
point=x2+lp2;
x=bun.indexOf("<item",point);
}
num=n;
}

String makeart(String bun,int point){
String sx1,sx2;
int x1,x2;
int lp1;
int lp2;
sx1="<item";
sx2="</item>";
lp1=sx1.length();
lp2=sx2.length();
x1=bun.indexOf(sx1,point);
x2=bun.indexOf(sx2,point);
return bun.substring(x1,x2+lp2);
}

void getdata(String name){
int nu;
int n;
String data[]=new String[1000];

nu=0;
try{
        URL url = new URL(name);

        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setRequestMethod("GET");
        con.connect();

        BufferedReader reader =
            new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
n=0;
        while (true){
            String line = reader.readLine();
            if ( line == null ){
                break;
            }
data[n]=line;
n=n+1;
nu=n;
        }
        reader.close();
        con.disconnect();
}
catch(Exception e) { System.err.println(e); }  
bun="";
for (n=0;n<nu;n++){
bun=bun+data[n];
}

}

}
最終更新:2011年03月27日 17:10