jeffy

BlogJava 首页 新随笔 联系 聚合 管理
  70 Posts :: 1 Stories :: 14 Comments :: 0 Trackbacks
  当用js的alert 方法显示ajax以responseText显示返回结果时候, 显示的是个xml结构文档, 但以responseXML解析xml的时候, 所有节点长度都为0,  这个问题关键是服务器端没有指定正确的文档格式:
        response.setContentType("text/xml;charset=UTF-8"); (正确)
         response.setContentType("text/html;charset=UTF-8");(错误)
posted on 2007-08-17 17:35 Live-in Java 阅读(2980) 评论(2)  编辑  收藏

评论

# re: Ajax以responseXML返回,客户端(IE)不能分析xml问题 2009-02-10 06:07 liyaxi
原文:http://jiangx.javaeye.com/blog/153066
1.对XMLHttpRequest请求返回的responseXML进行解析,responseXML是个XMLDOcument对象
假设返回的responseXML为:
<?xml version="1.0" encoding="UTF-8"
standal?>
<response>
<method>checkName</method>
<result>1</result>
</response>
则获取method和result值方法为:
var response = req.responseXML.documentElement;
method = response.getElementsByTagName('method')[0].firstChild.data;
result = response.getElementsByTagName('result')[0].firstChild.data;

2.创建一个XMLDocument对象
function getXMLDocument() {
var xDoc = null;
if (document.implementation && document.implementation.createDocument) {
xDoc = document.implementation.createDocument("", "", null);
} else {
if ((typeof ActiveXObject) != "undefined") {
var msXmlAx = null;
try {
msXmlAx = new ActiveXObject("Msxml2.DOMDocument");
}
catch (e) {
msXmlAx = new ActiveXObject("Msxml.DOMDocument");
}
xDoc = msXmlAx;
}
}
if (xDoc == null || typeof xDoc.load == "undefined") {
xDoc = null;
}
return xDoc;
}

3.创建一个DOM树
<people>
<person first-name="eric" middle-initial="h" last-name="jung">
<address street="321 south st" city="denver" state="co" country="usa" />
</person>
<person first-name="jed" last-name="brown">
<address street="321 north st" city="atlanta" state="ga" country="usa" />
<address street="321 south avenue" city="denver" state="co" country="usa" />
</person>
</people>
程序如下:
var doc=getXMLDocument();
var peopleElem = doc.createElement("people");
var personElem1 = doc.createElement("person");
personElem1.setAttribute("first-name", "eric");
personElem1.setAttribute("middle-initial", "h");
personElem1.setAttribute("last-name", "jung");

var addressElem1 = doc.createElement("address");
addressElem1.setAttribute("street", "321 south st");
addressElem1.setAttribute("city", "denver");
addressElem1.setAttribute("state", "co");
addressElem1.setAttribute("country", "usa");
personElem1.appendChild(addressElem1);

var personElem2 = doc.createElement("person");
personElem2.setAttribute("first-name", "jed");
personElem2.setAttribute("last-name", "brown");

var addressElem3 = doc.createElement("address");
addressElem3.setAttribute("street", "321 north st");
addressElem3.setAttribute("city", "atlanta");
addressElem3.setAttribute("state", "ga");
addressElem3.setAttribute("country", "usa");
personElem2.appendChild(addressElem3);

var addressElem5 = doc.createElement("address");
addressElem5.setAttribute("street", "321 south avenue");
addressElem5.setAttribute("city", "denver");
addressElem5.setAttribute("state", "co");
addressElem5.setAttribute("country", "usa");
personElem2.appendChild(addressElem5);

peopleElem.appendChild(personElem1);
peopleElem.appendChild(personElem2);
doc.appendChild(peopleElem);
alert(doc.xml);//xml属性只对IE管用
  回复  更多评论
  

# re: Ajax以responseXML返回,客户端(IE)不能分析xml问题 2009-02-10 06:20 liyaxi
得到的是一个数组
var str=new Array();
var xmlDoc=xmlHttp.responseXML;
for(var i=0;i<xmlDoc.length;i++)
{
str[i]=xmlDoc.getElementsByTagName("name")[i].childNodes [0].nodeValue;

}
注意要判断ie text() 和火狐的textContent()区别  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: