在HTML中,怎样用XML去解析一个XML文件?

2024-11-17 12:43:52
推荐回答(1个)
回答(1):


-
-






-



-







js:
// 加载xml文档
var xmlDoc;
function loadXML (xmlFile)
{
// 判断浏览器类型
if(window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null); xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
else
{
return null;
}
return xmlDoc;
}

function startload ()
{
var option,optiontext;
if(xmlDoc == null)
{
alert('您的浏览器不支持xml文件读取!');
}
else
{
var DeptXML = xmlDoc.getElementsByTagName("department");
//alert(DeptXML.length);
for (var i = 0 ; i < DeptXML.length ; i ++ )
{
option = document.createElement('option');
option.setAttribute("value",DeptXML[i].getAttribute('name'));
optiontext = document.createTextNode(DeptXML[i].getAttribute('name'));
option.appendChild(optiontext);
//option.innerHTML = DeptXML[i].getAttribute('name');
document.getElementById("cdepname").appendChild(option);
}
}
}

function clearList()
{
var username = document.getElementById("username");
while (username.childNodes.length >0)
{
username.removeChild(username.childNodes[0]);
}
}

function refreshList ()
{
clearList();
var option,optiontext;
if(xmlDoc == null)
{
alert('您的浏览器不支持xml文件读取!');
}
else
{
var selectDep = document.form1.cdepname.options[document.form1.cdepname.selectedIndex].text;
/*var selectDep =document.getElementById("cdepname").options[document.getElementById("cdepname").options.selectedIndex].value*/
//alert(selectDep);

var DeptXML = xmlDoc.getElementsByTagName("department");

for (var i = 0 ; i < DeptXML.length ; i ++ )
{
if (DeptXML[i].getAttribute("name") == selectDep)
{
var DeptSel = DeptXML[i];
break;
}
}

for (i=0 ; i < DeptSel.childNodes.length; i++)
{
chen = new Option(DeptSel.childNodes[i].getAttribute("name"),DeptSel.childNodes[i].getAttribute("name"));
document.form1.username.options.add(chen);
}
}
}

function CheckUser ()
{
//alert('sdfsadf')
if(xmlDoc == null)
{
alert('您的浏览器不支持xml文件读取!');
}
else
{
var selectDep = document.form1.cdepname.options[document.form1.cdepname.selectedIndex].text;

var DeptXML = xmlDoc.getElementsByTagName("department");

for (var i = 0 ; i < DeptXML.length ; i ++ )
{
if (DeptXML[i].getAttribute("name") == selectDep)
{
var DeptSel = DeptXML[i];
break;
}
}

var selectPer = document.form1.username.options[document.form1.username.selectedIndex].text;

for (i=0 ; i < DeptSel.childNodes.length; i++)
{
if (DeptSel.childNodes[i].getAttribute("name") == selectPer)
{
if (DeptSel.childNodes[i].getAttribute("pwd") == document.getElementById("pwd").value)
{
//alert('登陆成功!');
document.getElementById("loadDiv").style.display = "";
document.getElementById("Login").style.display ="none";
setTimeout("self.location='Forum.html'",2000);
break;
}
else
{
alert('密码错误!');
break;
}
}
}
}
}