js XMLDOM解析xml 文档实例解析
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>js XMLDOM解析xml 文档实例解析</title>
<script type="text/javascript教程">
var xmlDoc=null;
if (window.ActiveXObject)
{
// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{
// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("a.xml");
}
var s = xmlDoc.getElementsByTagName("Objects");
var music = xmlDoc.selectNodes("BADI/Objects/Object[@ElementType='music']/Property/text()");
var tv = xmlDoc.selectNodes("BADI/Objects/Object[@ElementType='tv']/Property/text()");
for(var i=0;i<music.length;i++){
alert(music[i].text);
}
for(var i=0;i<tv.length;i++){
alert(tv[i].text);
}
</script>
xml 文档格式
<BADI>
<Objects>
<Object ElementType="tv">
<Property Name="Ma">www.111cn.net</Property>
<Property Name="Re">1</Property>
<Property Name="Rs">111cn.net</Property>
</Object>
</Objects>
</BADI>
</head>
<body>
</body>
</html>