用java去访问web service 提供的电视节目预告

思路:
1 用程序去访问 web service 服务.返回 xml文件。
2 用dom 去解析xml
3 用解析后的数据 生成 html文件

访问web service 的代码
public class StaticHTMLFile{
public static boolean PrintPage(String page, String url_addr) {
URL url;
String rLine = null;
PrintWriter fileOut = null;
InputStream ins = null;
File file=new File(page.trim());

try {
url = new URL(url_addr);
ins = url.openStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(ins,
"utf-8"));//获取编码为utf-8的文件
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(out, "utf-8");
fileOut = new PrintWriter(writer);
//循环取取数据,并写入目标文件中
while ( (rLine = bReader.readLine()) != null) {
String tmp_rLine = rLine;
int str_len = tmp_rLine.length();
if (str_len > 0) {
fileOut.println(tmp_rLine);
fileOut.flush();
}
tmp_rLine = null;
}
url = null;
return true;
} catch (IOException e) {
System.out.println("error: " + e.getMessage());
e.printStackTrace();
return false;
} catch (Exception es) {
System.out.println(es.getMessage());
return false;
}
finally {//关闭资源
fileOut.close();
try {
ins.close();
} catch (IOException ex){
//关闭输入流出错
ex.printStackTrace();
}
}


}



}



把xml解析成int[]
public class DomXml2Int {

String fileName=null;//xml的文件
String tagName=null;//要提取的xml中标签的名字





public String getFileName() {
return fileName;
}


public void setFileName(String fileName) {
this.fileName = fileName;
}


public String getTagName() {
return tagName;
}


public void setTagName(String tagName) {
this.tagName = tagName;
}





public List<Integer> builder() throws ParserConfigurationException, SAXException, IOException{

List<Integer> ids=null;
ids=new ArrayList<Integer>();

DocumentBuilderFactory dbf=null;
DocumentBuilder db=null;
Document d=null;
NodeList nl=null;
Node n=null;
dbf=DocumentBuilderFactory.newInstance();
db=dbf.newDocumentBuilder();
d=db.parse(fileName);
nl=d.getElementsByTagName(tagName);
//循环取出每个Node的值
for(int i=0;i<nl.getLength();i++){
n=nl.item(i);//得到每个node
//System.out.println(n.getTextContent());
ids.add(Integer.valueOf(n.getTextContent()));

}



return ids;
}



}



提取电视节目的代码

public class DomXml2String {

/**
* @param args
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

System.out.println(new DomXml2String().builder("tt.xml")[0]);
System.out.println(new DomXml2String().builder("tt.xml")[1]);




}

//解析xml到数组中去

public String[] builder( String xmlFilePath ) throws ParserConfigurationException, SAXException, IOException{
String[] tvShow= new String[2];
StringBuffer tvShowTempAM=new StringBuffer();
StringBuffer tvShowTempPM=new StringBuffer();
DocumentBuilderFactory dbf=null;
dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=null;
db=dbf.newDocumentBuilder();
Document d=null;
d=db.parse(new File(xmlFilePath));
NodeList n=null;
n=d.getElementsByTagName("tvProgramTable");
Node node=null;
//循环得到每个节点的值
for(int i=0;i<n.getLength();i++){
node=n.item(i);
//System.out.println(i+node.getChildNodes().item(3).getTextContent() );
if(node.getChildNodes().item(3).getTextContent().equals("AM")){

//System.out.println(i+" is am" );
tvShowTempAM.append(node.getChildNodes().item(1).getTextContent()+""+node.getChildNodes().item(5).getTextContent()+"<br>");


}else{
// System.out.println(i+" is pm" );

tvShowTempPM.append(node.getChildNodes().item(1).getTextContent()+""+node.getChildNodes().item(5).getTextContent()+"<br>");

}

}


tvShow[0]="AM\n"+tvShowTempAM.toString();
tvShow[1]="PM\n"+tvShowTempPM.toString();



return tvShow;
}


}


提取湖北省的所以电视节目
public class TVRequestWebServiceTest {

/**
* @param args
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @throws TemplateException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TemplateException {

List<Integer> dstIds=null;


List<Integer> pdIds=null;
String[] tvShow= new String[2];
//根据省的id得到省的所有电视台的id(数据保存到dstIds.xml)
StaticHTMLFile.PrintPage("E:/workspace/spring2Test/dstIds.xml",
"http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx/getTVstationDataSet?theAreaID=17 "

);



//根据dstIds.xml得到电视台的id(int)
DomXml2Int dxi=null;
dxi=new DomXml2Int();
dxi.setFileName("dstIds.xml");

dxi.setTagName("tvStationID");
dstIds=dxi.builder();
DomXml2String dx=null;
dx=new DomXml2String();
String2Html s2h=null;


//循环每个电视台得到每个电视的所以频道id
for(int i=0;i<dstIds.size();i++){


StaticHTMLFile.PrintPage("E:/workspace/spring2Test/pdIds" +
"_" +
dstIds.get(i) +
".xml",
"http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx/getTVchannelDataSet?theTVstationID=" +
dstIds.get(i)
);
dxi.setFileName("pdIds_"+dstIds.get(i)+".xml");
dxi.setTagName("tvChannelID");
pdIds=dxi.builder();//获得每个电视台的所以频道id
//测试
/*for(int j=0;j<pdIds.size();j++){
System.out.println(dstIds.get(i)+":"+pdIds.get(j));

}
//测试成功
*/
//循环每个频道得到频道的电视节目
for(int j=0;j<pdIds.size();j++){

StaticHTMLFile.PrintPage("E:/workspace/spring2Test/pdIds" +
"_" +
dstIds.get(i)+
"_" +
pdIds.get(j)
+

".xml",
"http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx/getTVprogramDateSet?theTVchannelID=" +
pdIds.get(j)+
"&theDate=" +
"2009-02-20" +
"&userID="

);
tvShow= dx.builder("pdIds_"+dstIds.get(i)+"_"+pdIds.get(j)+".xml");
//测试 tvShow
/*System.out.println(tvShow[0]);
System.out.println(tvShow[1]);

*测试成功
AM
07:00晨曲
07:03重播:美嘉购物
14:00重播:都市商报
14:30重播:美嘉购物

PM
18:15元富财经
18:37都市商报
19:09股海罗盘
19:30湖北新闻联播
19:51天气预报
19:55直播:美嘉购物
23:55重播:美嘉购物
02:00夜曲

*
*
*/

//把tvshow的数据输出到html()
s2h.builderHtml(tvShow, dstIds.get(i), pdIds.get(j));




}





}



}

}

把数据转换成html的代码
public class String2Html {

public static void builderHtml(String[] strS,int i,int j) throws IOException, TemplateException{

//初始化freeMarker
File file=null;
file=new File("E:\\workspace\\spring2Test\\htmlTest\\");
Configuration cf=null;
cf=new Configuration();
cf.setDirectoryForTemplateLoading(file);
Template template=null;
template=cf.getTemplate("tvShow.ftl");
Map<String,String[]> root=null;
root=new HashMap<String,String[]>();
root.put("tvShow", strS);
Writer out=null;
//下面的文件名: 电视台id_频道id_时间.html
out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("E:/workspace/spring2Test/htmlTest/"+i+"_"+j+"_"+".html")),"utf-8"));

template.process(root, out);





}

}



//这里是原始的代码,需要好好整理整理。最好用soap
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值