servlet中out.println()的xml无法在ajax获取到
out.println("<?xmlversion='1.0'encoding='gb2312'?>");out.println("<stunames>");for(in...
out.println("<?xml version='1.0' encoding='gb2312'?>");
out.println("<stunames>");
for(int i=0;i<Al.size();i++){
xxinfo xx=(xxinfo)Al.get(i);
String stuname=xx.getName();
out.println("<stuname>"+stuname+"</stuname>");
}
out.println("<stun>"+"1213"+"</stun>");
out.println("</stunames>");
------------------servlet中的输出----------------------
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
var xmlDom=xmlHttp.responseXml;
var xxinfos=xmlDom.getElementsByTagName("stun");
alert(xxinfos.length);
---------------------此处alert的输出为0---------------------- 展开
out.println("<stunames>");
for(int i=0;i<Al.size();i++){
xxinfo xx=(xxinfo)Al.get(i);
String stuname=xx.getName();
out.println("<stuname>"+stuname+"</stuname>");
}
out.println("<stun>"+"1213"+"</stun>");
out.println("</stunames>");
------------------servlet中的输出----------------------
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
var xmlDom=xmlHttp.responseXml;
var xxinfos=xmlDom.getElementsByTagName("stun");
alert(xxinfos.length);
---------------------此处alert的输出为0---------------------- 展开
1个回答
展开全部
你输出的只不过是一堆类似xml文件的一些字符串而已,并不是xml数据。
LOG.info("******dom4j start create xml document stream*******");
Document doc = DocumentHelper.createDocument();
Element rootElement = doc.addElement("students");
Element pageElement = rootElement.addElement("page");
pageElement.setText(isreset);
rootElement.addComment("all students is here!");
if(stus != null && !stus.isEmpty()){
LOG.info("*******students number:" + stus.size() + "**********");
for(int i=0;i<stus.size();i++){
Element studentElement = rootElement.addElement("student");
Element id = studentElement.addElement("id");
Element name = studentElement.addElement("name");
Element age = studentElement.addElement("age");
Element sex = studentElement.addElement("sex");
Element country = studentElement.addElement("country");
id.setText(stus.get(i).getId() + "");
name.setText(stus.get(i).getName());
age.setText(stus.get(i).getAge() + "");
String sexName = stus.get(i).getSex();
sex.setText(sexName);
country.setText(stus.get(i).getCountry());
}
LOG.info("******dom4j success create xml document*******");
}else{
LOG.info("******get no student*******");
}
resp.setContentType("text/xml;charset=utf-8");
resp.setHeader("pragma","no-cache");
resp.setHeader("cache-control","no-cache");
PrintWriter out = resp.getWriter();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter xmlWriter = new XMLWriter(out,format);
xmlWriter.write(doc);
out.flush();
out.close();
LOG.info("******dom4j start create xml document stream*******");
Document doc = DocumentHelper.createDocument();
Element rootElement = doc.addElement("students");
Element pageElement = rootElement.addElement("page");
pageElement.setText(isreset);
rootElement.addComment("all students is here!");
if(stus != null && !stus.isEmpty()){
LOG.info("*******students number:" + stus.size() + "**********");
for(int i=0;i<stus.size();i++){
Element studentElement = rootElement.addElement("student");
Element id = studentElement.addElement("id");
Element name = studentElement.addElement("name");
Element age = studentElement.addElement("age");
Element sex = studentElement.addElement("sex");
Element country = studentElement.addElement("country");
id.setText(stus.get(i).getId() + "");
name.setText(stus.get(i).getName());
age.setText(stus.get(i).getAge() + "");
String sexName = stus.get(i).getSex();
sex.setText(sexName);
country.setText(stus.get(i).getCountry());
}
LOG.info("******dom4j success create xml document*******");
}else{
LOG.info("******get no student*******");
}
resp.setContentType("text/xml;charset=utf-8");
resp.setHeader("pragma","no-cache");
resp.setHeader("cache-control","no-cache");
PrintWriter out = resp.getWriter();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter xmlWriter = new XMLWriter(out,format);
xmlWriter.write(doc);
out.flush();
out.close();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询