ajax傳輸xml檔案樣本:具體參考上傳的ajax的xml格式傳輸.rar0
HjxxServlet中:
public class HjxxServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml;charset=UTF-8");
//取得日期
String rq = request.getParameter("rq");
KhfxxtManager khfxxtManager = new KhfxxtManager();
//取得呼叫統計資訊
Hjtj hjtj = khfxxtManager.getHjtj(rq);
Document doc = DocumentHelper.createDocument();
doc.setXMLEncoding("utf-8");
Element hjxxElt = doc.addElement("hjxx");
Element hjtjElt = hjxxElt.addElement("hjtj");
//今日呼叫量
hjtjElt.addElement("jrhjl").setText(String.valueOf(hjtj.getYxth() + hjtj.getWxth()));
//有效通話
hjtjElt.addElement("yxth").setText(String.valueOf(hjtj.getYxth()));
//無效通話
hjtjElt.addElement("wxth").setText(String.valueOf(hjtj.getWxth()));
//有效通話成功率
hjtjElt.addElement("yxthcgl").setText(String.valueOf(hjtj.getYxth()/(hjtj.getYxth() + hjtj.getWxth())));
String zt = null;
try {
long time = new SimpleDateFormat("yyyy-MM-dd").parse(rq).getTime() - (1000*60*60*24);
//取得昨天
zt = new SimpleDateFormat("yyyy-MM-dd").format(new Date(time));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//取得昨天呼叫統計資訊
Hjtj ztHjtj = khfxxtManager.getHjtj(zt);
//有效通話成功率同比昨日
hjtjElt.addElement("yxthcgltbzr").setText(String.valueOf((hjtj.getYxth()/(hjtj.getYxth() + hjtj.getWxth()))-(ztHjtj.getYxth()/(ztHjtj.getYxth() + ztHjtj.getWxth()))));
//潛在意向客戶
hjtjElt.addElement("qzyxkh").setText(String.valueOf(hjtj.getQzyxkh()));
//潛在意向客戶同比昨日
hjtjElt.addElement("qzyxkhtbzr").setText(String.valueOf(hjtj.getQzyxkh() - ztHjtj.getQzyxkh()));
//取得明細資訊
List<Hjmx> hjmxList = khfxxtManager.getHjmxList(rq);
//呼叫明細
Element hjmxElt = hjxxElt.addElement("hjmx");
for (Iterator<Hjmx> iter=hjmxList.iterator(); iter.hasNext();) {
Hjmx hjmx = iter.next();
Element hjjlElt = hjmxElt.addElement("hjjl");
//原始號碼
hjjlElt.addElement("yshm").setText(hjmx.getKhxx().getDhhm());
//呼叫情況
hjjlElt.addElement("hjqk").setText(hjmx.getHjqk());
//姓名
hjjlElt.addElement("xm").setText(hjmx.getKhxx().getXm());
}
String xmlString = doc.asXML();
response.getWriter().print(xmlString);
}
}
index.jsp中:
<script>
WdatePicker({eCont:'div1',onpicked:function(dp){
//alert(dp.cal.getDateStr())
var xmlHttp;
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "servlet/HjxxServlet?rq=" + dp.cal.getDateStr() + "×tamp=" + new Date().getTime();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange=function() {
if(xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
//alert(xmlHttp.responseText);
var doc = xmlHttp.responseXML;
//今日呼叫量
var jrhjl= doc.getElementsByTagName("jrhjl")[0].firstChild.nodeValue;
//有效通話
var yxth= doc.getElementsByTagName("yxth")[0].firstChild.nodeValue;
//無效通話
var wxth= doc.getElementsByTagName("wxth")[0].firstChild.nodeValue;
//有效通話成功
var yxthcgl= doc.getElementsByTagName("yxthcgl")[0].firstChild.nodeValue;
//有效通話成功率同比昨日
var yxthcgltbzr= doc.getElementsByTagName("yxthcgltbzr")[0].firstChild.nodeValue;
//潛在意向客戶
var qzyxkh= doc.getElementsByTagName("qzyxkh")[0].firstChild.nodeValue;
//潛在意向客戶同比昨日
var qzyxkhtbzr= doc.getElementsByTagName("qzyxkhtbzr")[0].firstChild.nodeValue;
document.getElementById("hjtj_div").innerHTML="<br />今日外呼量:" + jrhjl + "<br />有效通話:" + yxth + "呼<br /> 無效通話:" + wxth + "呼<br /> 有效通話成功率:" + yxthcgl + "%同比昨日:" + yxthcgltbzr + "%<br /> 潛在意向客戶數:" + qzyxkh + "位同比昨日" + qzyxkhtbzr + "%<br
/>";
//取得明細
var hjjl = doc.getElementsByTagName("hjjl");
var s = "<table border='2' width='750px;' height='300px' cellspacing='8px'>\n";
s+="<tr align='center' bgcolor='#CCCCCC'>\n";
s+="<th width='109'>原始號碼</th>\n";
s+="<th width='109' >呼叫情況</th>\n";
s+="<th width='109' >姓名</th>\n";
s+="</tr>\n";
for (var i=0; i<hjjl.length; i++) {
var yshm = hjjl[i].childNodes[0].firstChild.nodeValue;
var hjqk = hjjl[i].childNodes[1].firstChild.nodeValue;
var xm = hjjl[i].childNodes[2].firstChild.nodeValue;
s+="<tr bgcolor='#CCCCCC' align='center'>\n";
s+="<td>" + yshm + "</td>\n"
s+="<td>" + hjqk + "</td>\n"
s+="<td>" + xm + "</td>\n"
s+="</tr>\n";
}
s+="</table>";
document.getElementById("hjmx_div").innerHTML = s;
}else {
alert("請求失敗,錯誤碼=" + xmlHttp.status);
}
}
};
xmlHttp.send(null);
}})
</script>