直接在response中生成xml文件

package com.lenovomobile.wap;

import com.lenovomobile.wap.auto.FaceConfig;
import com.lenovomobile.wap.domain.Column;
import com.lenovomobile.wap.domain.logic.WapFacade;
import com.lenovomobile.wap.help.Base64;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.net.URLEncoder;
import java.util.List;
import java.util.Vector;

/**
* Created by IntelliJ IDEA.
* User: wangqiaowqo
* Date: 2009-5-21
* Time: 16:46:26
* 该类的用途:生成XML文件.
*/
public class GenXmlController implements Controller {

private WapFacade wap;

public void setWap(WapFacade wap) {
this.wap = wap;
}

int currentpoint = 0;

public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {

Vector<Column> table = new Vector<Column>();
Vector<Column> queue = new Vector<Column>();
Vector<Column> shows = new Vector<Column>();

currentpoint = (int) (Math.random() * 16);

FaceConfig config = FaceConfig.getInstance();
String s = config.getPropString("default");

String ids[] = s.split(",");

for (String id : ids) {
if (id == null || id.length() == 0 || !id.matches("\\d+")) {
continue;
}

List columns = this.wap.getProductList(id);
for (Object o : columns) {
table.add((Column) o);
}
}

if (table.size() > 0) {
synchronized (queue) {
queue.removeAllElements();
queue.addAll(table);
}
}

String ringId = config.getPropString("column_ring_num");
Vector<Column> queueList = new Vector<Column>();
Vector<Column> ringList = new Vector<Column>();
for (int j = 0; j < queue.size(); j++) {
if (ringId.equals(queue.elementAt(j).getParentId())) {
ringList.add(queue.elementAt(j));
} else {
queueList.add(queue.elementAt(j));
}
}

if (queueList.size() > 0) {
for (int i = 0; i < 3; i++) {
int index = currentpoint + i;
index = index % queueList.size();
shows.add(queueList.elementAt(index));
}
}

if (ringList.size() > 0) {
for (int i = 0; i < 1; i++) {
int index = currentpoint + i;
index = index % ringList.size();
shows.add(ringList.elementAt(index));
}
}

//以下为定义Document有关变量
Document doc = null;
Element root = null;
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;

//以下为确定输出类型,定义输出变量
response.setContentType("text/html;charset=utf-8");

//PrintWriter out=response.getWriter();

//初始化各DocumentBuilderFactory变量
try {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
doc = builder.newDocument();
root = doc.createElement("yule");

for(Column show : shows){
Element article = doc.createElement("article");
if (show.getWideName() != null){
article.setAttribute("title", show.getWideName());
} else {
article.setAttribute("title", show.getProductName());
}
article.setAttribute("url", url(show));
root.appendChild(article);
}

doc.appendChild(root);

TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
DOMSource dom = new DOMSource(doc);
StreamResult sr = new StreamResult(response.getOutputStream());
t.setOutputProperty("encoding", "UTF-8");
//StringWriter sw=new StringWriter();
//StreamResult sr=new StreamResult(sw);
t.transform(dom, sr);

} catch (Exception e) {
//out.println("出错提示:"+e.getMessage());
}

return null;
//out.close();
}

private String url(Column c) throws Exception {
String base = "r4log.do?url="
+ URLEncoder.encode(Base64.encode(c.getProductUrl()), "utf-8");

return base += "&pid=" + c.getProductId() + "&cid="
+ c.getParentId();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值