论坛首页 Web前端技术论坛

responseXML为空?

浏览 11632 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-01-11  
最近不怎么忙,正用dhtmlXGrid搞一个自己的小系统!
前一个页面都正常,前天做一个新页面的时候出现了问题:页面不能正常解析服务器端返回的数据,总是报Incorrect XML的错误;

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

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

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

发送请求(来自dhtmlXCommon.js)
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;
};


////////////////////////////////////////
//下面是取首节点,其中tagName为"rows"
出错页面调用时,我用alert(this.xmlDoc.responseXML.xml)查看的到是空值;用alert(this.xmlDoc.responseText)查看得到的是本页面的html文件;而在没有错误的页面得到的都是服务器传回的XML文件。
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");
};



这是我生成xml文件的类XmlUtil.java
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)
			{
			}
		}
	}
}



这是在service层的处理方法
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());
	}
}


其他的地方没有什么特别的了;
我在网上搜了一下,有网友对这种问题总结了四种错误:
调试此问题的四个步骤:
     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时间不长,还请各位多多帮忙!
   发表时间:2007-01-11  
我也想学习这方面的实例,你可不可以把上面的资料打个包共享出来阿?先谢谢了。
0 请登录后投票
   发表时间:2007-01-12  
现在前几个页面中也或多或少的存在一些bug.而且我还没搞清楚是我的代码有问题还是dhtmlXGrid(免费版)的问题,问题解决了我会把代码共享出来的!

0 请登录后投票
   发表时间:2007-01-12  
我以前也碰到responseXML为空的问题,但是responseText不为空,后来我就统一用responseText作为返回值了,当然,我用的也不是XML格式。
0 请登录后投票
   发表时间:2007-01-12  
dhtmgrid如果返回的是text,它会自己把它转化成document对象的,构建XML对象,建议使用dom4j,很方便的说
dtmlXMLLoaderObject.prototype.loadXMLString=function(xmlString){
 try
{
 var parser = new DOMParser();
 this.xmlDoc = parser.parseFromString(xmlString,"text/xml");
}
 catch(e){
 this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 this.xmlDoc.async=this.async;
 this.xmlDoc.loadXML(xmlString);
}
 this.onloadAction(this.mainObject,null,null,null,this);
 if(this.waitCall){this.waitCall();this.waitCall=null;}
}
0 请登录后投票
   发表时间:2007-01-12  
Ivan Li 写道
dhtmgrid如果返回的是text,它会自己把它转化成document对象的,构建XML对象,建议使用dom4j,很方便的说
dtmlXMLLoaderObject.prototype.loadXMLString=function(xmlString){
 try
{
 var parser = new DOMParser();
 this.xmlDoc = parser.parseFromString(xmlString,"text/xml");
}
 catch(e){
 this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 this.xmlDoc.async=this.async;
 this.xmlDoc.loadXML(xmlString);
}
 this.onloadAction(this.mainObject,null,null,null,this);
 if(this.waitCall){this.waitCall();this.waitCall=null;}
}


哦!谢谢!我再搞搞看看!
0 请登录后投票
   发表时间:2007-01-15  
问题解决了!
是我在服务器端进行数据校验后,使用springMVC页面转向的问题!
0 请登录后投票
   发表时间:2007-07-31  
能不能告诉我怎么解决这个问题?
0 请登录后投票
   发表时间:2007-08-03  
其实很简单,是一个低级错误;我在转发页面的时候,应该return null.
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics