responseXML为空?

最近不怎么忙,正用dhtmlXGrid搞一个自己的小系统!
前一个页面都正常,前天做一个新页面的时候出现了问题:页面不能正常解析服务器端返回的数据,总是报Incorrect XML的错误;

百度了一下,又进行了调试得知是receponseXML为空导致的。可是我前一个页面也是使用的同一个Util类生成的XML文件的啊!
由于需要的xml文件比较简单,也没有采用xml文件的生成工具,这下麻烦死了。

从昨天就开始找问题所在,搞得头都大了,也还是没戏!

将重要部分的代码贴出来,请各位帮忙瞅瞅!

发送请求(来自dhtmlXCommon.js)
[code]
dtmlXMLLoaderObject.prototype.loadXML = function (filePath, postMode, postVars) {
if (this.rSeed) {
filePath += ((filePath.indexOf("?") != -1) ? "&" : "?") + "a_dhx_rSeed=" + (new Date()).valueOf();
}
this.filePath = filePath;
alert(this.filePath);
if (window.XMLHttpRequest) {
this.xmlDoc = new XMLHttpRequest();
this.xmlDoc.open(postMode ? "POST" : "GET", filePath, this.async);
if (postMode) {
this.xmlDoc.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
this.xmlDoc.onreadystatechange = new this.waitLoadFunction(this);
this.xmlDoc.send(null || postVars);
} else {
if (document.implementation && document.implementation.createDocument) {
this.xmlDoc = document.implementation.createDocument("", "", null);
this.xmlDoc.onload = new this.waitLoadFunction(this);
this.xmlDoc.load(filePath);
} else {
this.xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
this.xmlDoc.open(postMode ? "POST" : "GET", filePath, this.async);
if (postMode) {
this.xmlDoc.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
this.xmlDoc.onreadystatechange = new this.waitLoadFunction(this);
this.xmlDoc.send(null || postVars);
}
}
};

///
dtmlXMLLoaderObject.prototype.waitLoadFunction = function (dhtmlObject) {
this.check = function () {
if (dhtmlObject.onloadAction != null) {
if ((!dhtmlObject.xmlDoc.readyState) || (dhtmlObject.xmlDoc.readyState == 4)) {
dhtmlObject.onloadAction(dhtmlObject.mainObject, null, null, null, dhtmlObject);
if (dhtmlObject.waitCall) {
dhtmlObject.waitCall();
dhtmlObject.waitCall = null;
}
dhtmlObject = null;
}
}
};
return this.check;
};
[/code]


//下面是取首节点,其中tagName为"rows"
出错页面调用时,我用alert(this.xmlDoc.responseXML.xml)查看的到是空值;用alert(this.xmlDoc.responseText)查看得到的是本页面的html文件;而在没有错误的页面得到的都是服务器传回的XML文件。
[code]
dtmlXMLLoaderObject.prototype.getXMLTopNode = function (tagName) {
if (this.xmlDoc.responseXML) {
var temp = this.xmlDoc.responseXML.getElementsByTagName(tagName);
var z = temp[0];
} else {
var z = this.xmlDoc.documentElement;
}
if (z) {
this._retry = false;
return z;
}
if ((_isIE) && (!this._retry)) {
var xmlString = this.xmlDoc.responseText;
this._retry = true;
this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
this.xmlDoc.async = false;
this.xmlDoc.loadXML(xmlString);
return this.getXMLTopNode(tagName);
}
dhtmlxError.throwError("LoadXML", "Incorrect XML", [this.xmlDoc, this.mainObject]);
return document.createElement("DIV");
};
[/code]


这是我生成xml文件的类XmlUtil.java
[code]
package pfm.web.util;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class XmlUtil
{
StringBuffer xmlBuffer = new StringBuffer();

HttpServletRequest request = null;

HttpServletResponse response = null;

public XmlUtil(HttpServletRequest request, HttpServletResponse response)
{
this.request = request;
this.response = response;
xmlBuffer.append("<?xml version='1.0' encoding='UTF-8'?>");
xmlBuffer.append("<rows>");
xmlBuffer.append("</rows>");
}

public void setId(String id)
{
xmlBuffer.insert(xmlBuffer.lastIndexOf("</rows>"), "<row id='" + id + "'></row>");
}

public void setXmlData(Object cell)
{
if (xmlBuffer.lastIndexOf("</row>") > 0)
xmlBuffer.insert(xmlBuffer.lastIndexOf("</row>"), "<cell>" + cell + "</cell>");
}

public String getXmlFile()
{
return new String(xmlBuffer);
}

/**
* 从请求的输入流中读取并解析数据
*/
public List getXmlData()
{
List l = new ArrayList();
try
{
SAXBuilder xmlbuiilder = new SAXBuilder();
BufferedInputStream buinst = new BufferedInputStream(request.getInputStream());
Document document = xmlbuiilder.build(buinst);
Element root = document.getRootElement();
for (Iterator it = root.getChildren("row").iterator(); it.hasNext();)
{
Element row = (Element) it.next();
Object[] o = new Object[row.getChildren().size() + 1];
o[0] = row.getAttributeValue("id");
int i = 1;
if (!row.getChildren().isEmpty())
for (Iterator itcell = row.getChildren("cell").iterator(); itcell.hasNext();)
{
Element cell = (Element) itcell.next();
o[i++] = cell.getText();
}
l.add(o);
}

} catch (Exception e)
{
e.printStackTrace();
}
return l;
}

/**
* 将服务器响应以xml形式返回客户端
*/
public void returnResultXml(String resultxml)
{
System.out.println("resultxml==" + resultxml);
try
{
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(resultxml);
response.getWriter().flush();
} catch (IOException e)
{
e.printStackTrace();
try
{
response.getWriter().write("<flag>0</flag>");
} catch (Exception fe)
{
}
}
}
}

[/code]

这是在service层的处理方法
[code]
public class DailyReceiptsServiceImp implements DailyReceiptsService
{
private DailyReceiptsDao dailyReceiptsDao;

private HelpUtil help;

public void setDailyReceiptsDao(DailyReceiptsDao dailyReceiptsDao)
{
this.dailyReceiptsDao = dailyReceiptsDao;
}

public void buildQueryXML(HttpServletRequest request, HttpServletResponse response)
{
XmlUtil xmlutil = new XmlUtil(request, response);
help = new HelpUtil();

List list = this.dailyReceiptsDao.findTotal();
if (list != null)
{
for (Iterator it = list.iterator(); it.hasNext();)
{
DailyReceipts dailyReceipts = (DailyReceipts) it.next();
xmlutil.setId(dailyReceipts.getId().toString());
xmlutil.setXmlData(dailyReceipts.getIncomer());
xmlutil.setXmlData(dailyReceipts.getIncometype());
xmlutil.setXmlData(help.myDateFormat(dailyReceipts.getIncomedate()));
xmlutil.setXmlData(dailyReceipts.getCurrencytype());
xmlutil.setXmlData(dailyReceipts.getState());
xmlutil.setXmlData(help.chanToGB(dailyReceipts.getLocation()));
}
}
xmlutil.returnResultXml(xmlutil.getXmlFile());
}
}
[/code]

其他的地方没有什么特别的了;
我在网上搜了一下,有网友对这种问题总结了四种错误:
调试此问题的四个步骤:
1. 是否将 content type 设置为 text/xml 文件格式?
2. 是否确定将请求发送到服务器端了?
3. 输出 responseText,返回的内容是否是你想要的xml文件 ?
4. 在浏览器中直接输入返回 xml 文档的网址,检查一下返回的 xml 格式是否正确。

原文网址:
http://radio.javaranch.com/pascarello/2006/09/12/1158096122600.html

请求能够发送到服务器端,我在将xml返回前在服务器端输出了xml,显示正常,服务器端也能正常响应请求,输出的responseText不对,看来我就是第三种啊!

用ajax时间不长,还请各位多多帮忙!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值