六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 56|回复: 0

javascript解析xml文件(一)

[复制链接]

升级  59.33%

35

主题

35

主题

35

主题

秀才

Rank: 2

积分
139
 楼主| 发表于 2013-1-27 05:19:42 | 显示全部楼层 |阅读模式
页面文件已经能够包含html和js代码<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>demo1.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><script language="javascript" type="text/javascript">var xhai_xml;function xhai_GetXML(){try{     xhai_xml = new ActiveXObject("Microsoft.XMLDOM");    xhai_xml.async=true;    xhai_xml.onreadystatechange = GetReadyIE;    xhai_xml.load("datafile.xml"); }catch(Exception){ var record;xhai_xml = document.implementation.createDocument("","M2MDoc",null);xhai_xml.async=false;xhai_xml.load("datafile.xml");//alert(xhai_xml.getElementsByTagName("SPID")[0].firstChild.nodeValue);record = "SPID:"+xhai_xml.getElementsByTagName("SPID")[0].firstChild.nodeValue+"<br>";//alert(xhai_xml.getElementsByTagName("SPName")[0].firstChild.nodeValue);record = record + "SPName:"+ xhai_xml.getElementsByTagName("SPName")[0].firstChild.nodeValue + "<br>";record = record + "MovieID | MovieName | Class |<br>";//alert(xhai_xml.getElementsByTagName("Item").length);var items = xhai_xml.getElementsByTagName("Item");for(var i = 0 ; i < items.length ; i++){//alert(items[i].attributes.length);var attributes = items[i].attributes;for(var j = 0 ; j < attributes.length ; j++){//alert(attributes[j].value);record = record + attributes[j].value + "|";}record = record + "<br>";}var page_attributes = xhai_xml.getElementsByTagName("Page")[0].attributes;record = record + "Name:" + page_attributes[0].value+"  ";record = record + "AllPage" + page_attributes[1].value+"  ";record = record + "Count" + page_attributes[2].value+"  ";document.write(record); }}function GetReadyIE(){ if(xhai_xml.readyState==4){     if(xhai_xml.parseError.errorCode==0){    document.write("SPID:"+xhai_xml.documentElement.selectNodes("SPID")(0).text+"<br />");   document.write("SPName:"+xhai_xml.documentElement.selectNodes("SPName")(0).text+"<br />");   document.write("<br />");    var i,c,d,Line;    var MovieList = xhai_xml.documentElement.selectNodes("//MovieList/Item");   var j=MovieList.length;      Line = MovieList.item(0);  d=Line.attributes.length;  for(c=0;c<d;c++){   document.write(Line.attributes(c).name+" | ");   }  document.write("<br />");  for(i=0;i<j;i++){  Line = MovieList.item(i);    d=Line.attributes.length;    for(c=0;c<d;c++){    document.write(Line.attributes(c).value+" | ");    }    document.write("<br />");   }   document.write("<br />");   var page = xhai_xml.documentElement.selectNodes("//Page").item(0)   j=page.attributes.length;   for(i=0;i<j;i++){   document.write(page.attributes(i).name+":");   document.write(page.attributes(i).value+" ");   }}delete(xhai_xml);    }}xhai_GetXML();</script></head><body></body></html>datafile.xml<?xml version="1.0" encoding="gb2312"?><M2MDoc>    <SPID>3</SPID>    <SPName>5</SPName>    <MovieList>        <Item MovieID="103704" MovieName="忍" Class="动作片" />        <Item MovieID="103701" MovieName="风斗士" Class="动作片" />         <Item MovieID="103693" MovieName="无血无泪11" Class="动作片" />    </MovieList>    <Page Number="1" AllPage="3" Count="45"/></M2MDoc>  
 兼容IE和Firefox浏览器
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表