思路:
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
分享到:
相关推荐
最近在学习Web Service,发现了一个国内的Web Service提供站点,其中最简单的是查询QQ在线状态服务。我通过Java直接发送SOAP请求文件访问Web Service成功,这种方式实现比较简单,不需要第三方的软件包。
Java Web Service 学习例子,里面有大量用来开发web services的Java程序示例,非常值得学习。 使用到的技术及工具如下: Apache: www.apache.org and xml.apache.org -- Ant 1.4.1 -- SOAP 2.2 -- Tomcat 3.2...
java web service 教程java web service 教程java web service 教程java web service 教程java web service 教程java web service 教程java web service 教程java web service 教程java web service 教程java web ...
ASP.NET 中使用 C# 调用 Java Web Service ...在 ASP.NET 中使用 C# 调用 Java Web Service 需要了解 Web Service 的基本概念、使用 C# 调用 Java Web Service 和在 ASP.NET 中实现 Web Service 的客户端和服务器端。
JAVA程序调用Web Service示例过程演示.pdf
使用Java创建RESTful Web Service
JAVA实现web service
《Java RESTful Web Service实战》PDF版本下载
Java的Web Service支持Java的Web Service支持Java的Web Service支持
Java Web Service的客户端实现
Java RESTful Web Service实战.pdf Java RESTful Web Service实战.pdf
java web service, web service 客户端java例子,
XFire Java Web Service
Java+restful+Web+Service实战,完整版,非常值得拥有。
使用Java创建RESTful Web Service - ImportNew1
java模拟ie 访问web
创建和访问Web Service 1 1. 概述 2 2. 创建Java Web Service 2 3. 访问Java Web Service 9 4. 访问.NET Web Service 20
JAVA Web service 开发实例
web service soap协议详解,并有java代码事例
Web Service示例。不知道如何写WEB service程序,那就认真看看这个实例吧!