我的js函数:
function test(){
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("ff.xml");
//testNodes();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.async="false";
xmlDoc.load("ff.xml");
// xmlDoc.onload = testNodes;
}
// no good browser found
else
{
if (!xmlDoc.load("ff.xml"))
{
alert ("Failed to load XML data source!");
}
} alert(xmlDoc.getElementsByTagName("item").length); }
我的xml文件 <?xml version="1.0" encoding="UTF-8"?>
<tree>
<item text="1" id="62">
<item text="中" id="66">
<item text="121" id="73"/>
</item>
<item text="11" id="74">
<item text="111" id="75"/>
</item>
</item>
<item text="2" id="63">
<item text="21" id="72"/>
</item>
<item text="3" id="64"/>
</tree>
ie下正常弹出8,ff下始终是0,请各位老大指点指点!谢谢
function test(){
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("ff.xml");
//testNodes();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.async="false";
xmlDoc.load("ff.xml");
// xmlDoc.onload = testNodes;
}
// no good browser found
else
{
if (!xmlDoc.load("ff.xml"))
{
alert ("Failed to load XML data source!");
}
} alert(xmlDoc.getElementsByTagName("item").length); }
我的xml文件 <?xml version="1.0" encoding="UTF-8"?>
<tree>
<item text="1" id="62">
<item text="中" id="66">
<item text="121" id="73"/>
</item>
<item text="11" id="74">
<item text="111" id="75"/>
</item>
</item>
<item text="2" id="63">
<item text="21" id="72"/>
</item>
<item text="3" id="64"/>
</tree>
ie下正常弹出8,ff下始终是0,请各位老大指点指点!谢谢
var xmlDom; //XML DOM object
var xmlFile="ff.xml"; //xml file name
loadXML = function(fileRoute){
xmlDoc=null;
if (window.ActiveXObject){
xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
xmlDoc.async=false;
xmlDoc.load(fileRoute);
}else if (document.implementation && document.implementation.createDocument){
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET",fileRoute,false);
xmlhttp.send(null);
var xmlDoc = xmlhttp.responseXML;
}
else {xmlDoc=null;}
return xmlDoc;
}
xmlDom=loadXML(xmlFile);
var newsList2=xmlDom.getElementsByTagName("item");
alert(newsList2.length)
}楼主你把xml文件中那个唯一的中文“中”改成英文的,读出来的就是8了,用我的代码,你的js代码好像也有问题
大侠,你太厉害了。问题解决了。不过现在我的不是xml文件,是ajax返回的xmlHttp.responseXML,我把你的代码移植过去,问题依旧,还请继续赐教,谢谢
var req = new XMLHttpRequest();
req.open("GET","xml.jsp",false)
req.send(null)
var xmlDoc = req.responseXML
alert(xmlDoc.getElementsByTagName("item").length)
</script>xml.jsp
<%@page language="java" contentType="text/xml; charset=UTF-8"
pageEncoding="UTF-8"%><?xml version="1.0" encoding="UTF-8"?>
<tree>
<item text="1" id="62">
<item text="中" id="66">
<item text="121" id="73" />
</item>
<item text="11" id="74">
<item text="111" id="75" />
</item>
</item>
<item text="2" id="63">
<item text="21" id="72" />
</item>
<item text="3" id="64" />
</tree>
我就是这样写的,在IE下没问题,firfox下,读出来一直是0,另外,我用的php,xml文件的头<?xml version="1.0" encoding="utf-8"?>
function GetXmlHttpObject()
{
var objXMLHttp=null;
if(window.XMLHttpRequest){
objXMLHttp=new XMLHttpRequest()
}else if(window.ActiveXObject){
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp;
}//-----------------------------------------
function opendiv(t_id)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return
}
var url="getdiv.php";
url=url+"?t_id="+t_id;
xmlHttp.onreadystatechange=gCallBack;
xmlHttp.open("GET",url,false);
xmlHttp.send(null);
}//------------------------------------------------
function gCallBack()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
if (xmlHttp.status==200)
{
xmlDoc=xmlHttp.responseXML; alert(xmlDoc.getElementsByTagName("t_id").length);
}
}
}//--------------------------getdiv.php---------------------$t_id=$_GET["t_id"];
$ct_template = new ct_template($db);
$ct_template->getTemplate($t_id);
echo '<?xml version="1.0" encoding="GBK"?>';
echo '<ct_template>';
echo "<t_id id='t_id'>" . htmlspecialchars($ct_template->id) . "</t_id>";
echo "<name>" . htmlspecialchars($ct_template->name) . "</name>";
echo "<user_id>" . htmlspecialchars($ct_template->user_id) . "</user_id>";
echo "<creat_date>" . htmlspecialchars($ct_template->creat_date) . "</creat_date>";
echo "<modify_date>" . htmlspecialchars($ct_template->modify_date) . "</modify_date>";
echo "<t_re>" . htmlspecialchars($ct_template->re) . "</t_re>";
$ct_form = new ct_form($db);
$allForm = $ct_form->getForms("where t_id=".$t_id);
for($i=0;$i<count($allForm); $i++){
echo "<id>" . htmlspecialchars($allForm[$i]->id) . "</id>";
echo "<title>" . htmlspecialchars($allForm[$i]->title) . "</title>";
echo "<position>" . htmlspecialchars($allForm[$i]->position) . "</position>";
echo "<size>" . htmlspecialchars($allForm[$i]->size) . "</size>";
echo "<style>" . htmlspecialchars($allForm[$i]->style) . "</style>";
echo "<cc>" . htmlspecialchars($allForm[$i]->cc) . "</cc>";
echo "<mc>" . htmlspecialchars($allForm[$i]->mc) . "</mc>";
echo "<ad_pic_src>" . htmlspecialchars($allForm[$i]->ad_pic_src) . "</ad_pic_src>";
echo "<ad_pic_href>" . htmlspecialchars($allForm[$i]->ad_pic_href) . "</ad_pic_href>";
echo "<ad_pic_title>" . htmlspecialchars($allForm[$i]->ad_pic_title) . "</ad_pic_title>";
echo "<ad_pic_size>" . htmlspecialchars($allForm[$i]->ad_pic_size) . "</ad_pic_size>";
echo "<audit_style>" . htmlspecialchars($allForm[$i]->audit_style) . "</audit_style>";
echo "<sort_style>" . htmlspecialchars($allForm[$i]->sort_style) . "</sort_style>";
echo "<sort_style1>" . htmlspecialchars($allForm[$i]->sort_style1) . "</sort_style1>";
echo "<show_essence>" . htmlspecialchars($allForm[$i]->show_essence) . "</show_essence>";
echo "<show_style>" . htmlspecialchars($allForm[$i]->show_style) . "</show_style>";
echo "<show_rows>" . htmlspecialchars($allForm[$i]->show_rows) . "</show_rows>";
echo "<show_cols>" . htmlspecialchars($allForm[$i]->show_cols) . "</show_cols>";
echo "<show_charts>" . htmlspecialchars($allForm[$i]->show_charts) . "</show_charts>";
echo "<show_ellipsis>" . htmlspecialchars($allForm[$i]->show_ellipsis) . "</show_ellipsis>";
echo "<content_page>" . htmlspecialchars($allForm[$i]->content_page) . "</content_page>";
echo "<re>" . htmlspecialchars($allForm[$i]->re) . "</re>";
echo "<ad_pics>" . htmlspecialchars($allForm[$i]->ad_pics) . "</ad_pics>";
}
echo "</ct_template>";
其中php部分没有问题,我的alert(xmlHttp.responseText)在IE和firefox下都能正常打印出来
req.open("GET", "XMLForm.aspx", false); req.onreadystatechange = function()
{
if (req.readyState == 4 && req.status == 200)
{
var xmlDoc = req.responseXML;
alert(xmlDoc.getElementsByTagName("item").length);
}
}
req.send(null);
应该没有问题,你没有处理onreadystatechange 事件吧
这个ok,ff下通过返回8
var request;
if (window.XMLHttpRequest)
{
request = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("当前浏览器不支持AJAX");
}
//初始化对象 var url = "XMLForm.aspx";
request.open("GET", url, true); request.onreadystatechange = function()
{ if (request.readyState == 4)
{ //请求已成功返回
if (request.status == 200)
{ //请求正常
//获取结果
var xmlDoc = request.responseXML;
alert(xmlDoc.getElementsByTagName("item").length);
}
} }
request.send(null); //发送请求
我用的req.open("POST","xml.jsp",true)要不然在firefox下,连responseText都不返回
加个QQ吧,请教一下,我的QQ53893439
req.open("GET", "XMLForm.aspx", false);
req.send(null); if (req.status == 200 || req.status == 304)
{
var xmlDoc = req.responseXML; alert(xmlDoc.getElementsByTagName("item").length);
}
else
{
alert('XML request error: ' + req.statusText + ' (' + oXmlHttp.status + ')');
}
https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest