欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

itunes 获取 AppStore Top100

程序员文章站 2022-04-12 10:33:55
...
主要是想写读取图片自定义标签的。
写完以后 突然想做件奇怪事 通过自定义标签读取 AppStore Top 100.



package tag;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class AppTopTag extends TagSupport {

private String url ;
public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

/**
* httpjson
* @Title: AppTopTag.java
* @Package tag
* @Description: TODO
* @author liazhou
* @date 2011-11-7 23:11:11
* @version V1.0
*/
private static final long serialVersionUID = 8253601591021627633L;

@SuppressWarnings("unchecked")
public int doEndTag() throws JspException {
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
try {
this.pageContext.getOut().print("<title>ax.itunes.apple.com >AppTop 100</title>");
httpClient.executeMethod(postMethod);
//获取二进制的byte流
//byte[] b = postMethod.getResponseBody();
InputStream xmlStream = postMethod.getResponseBodyAsStream();
InputStreamReader xmlStreamReader = new InputStreamReader(xmlStream, "UTF-8");
if (xmlStreamReader != null) {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlStream);
document.setXMLEncoding("UTF-8");
Element root = document.getRootElement();
StringBuffer sbhtml = new StringBuffer();
//找出所有的entry
for(Iterator<Element> i = root.elementIterator("entry");i.hasNext();){
Element element = i.next();
Element elementc = element.element("content");
String en = elementc.getText();
if(null!=en && !"".equals(en))
sbhtml.append(en);
}
this.pageContext.getOut().print(sbhtml);
// 遍历子节点元素
}
}catch (Exception e) {
e.getMessage();
}
return EVAL_PAGE;
}

public int doStartTag() throws JspException {
// TODO Auto-generated method stub
return Tag.SKIP_BODY; //不处理正文内容
}
}





[b][color=red][size=large]apptop.jsp[/size][/color][/b]

<%@taglib uri="/union-ui-tag" prefix="union-ui"%>
<union-ui:appTop url="http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/sf=143441/limit=100/xml"></union-ui:appTop>



union-tags.tld


<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">


<display-name>"Union Tags"</display-name>
<tlib-version>1.0</tlib-version>
<short-name>union-ui</short-name>
<uri>/union-ui-tag</uri>
<tag>
<description><![CDATA[Execute an action from within a view]]></description>
<name>img</name>
<tag-class>tag.ImageTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description></description>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>appTop</name>
<tag-class>tag.AppTopTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>url</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>



页面截图