二、JS版源代码
==================================================
<!--
刘海龙,stan
[email]xiaoleilong@mail.biti.edu.cn[/email]
2003-5-9整理
-->
<HTML>
<HEAD>
<TITLE>XML Src Viewer</TITLE>
<STYLE TYPE="text/css">
BODY {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
</STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" onload="parseXML()">
<SPAN CLASS="heading">XML 源码察看器 </SPAN><
br>
<form name="form1" enctype="multipart/form-data" method="post" action="">
<input name="file" type="file" onChange="parseXML()" value="test.xml">
</form>
<HR>
<!--------------------------------------------------------------------------->
<SPAN ID="txtData"></SPAN>
<SCRIPT LANGUAGE="JScript">
function parseXML()
{
var domXMLData=new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
var strFilePath;
strFilePath = document.form1.file.value;
if(strFilePath=="")
txtData.innerHTML='<p><font color=\"red\"><b>请浏览选择需要察看的xml文件. :)</b></font> </p>' ;
else
{
domXMLData.load(strFilePath);
if (domXMLData.parseError.errorCode != 0)
{
txtData.innerHTML='<p><font color=\"red\"><b>Invalid XML file:</b></font> ' + domXMLData.parseError.reason+"</p>";
return;
}
//递归遍历不同类型子节点,加上高亮标记 (<font color=#> </font>)
txtData.innerHTML = renderChildNodes(domXMLData, 0);
}
}
var g_strNodeTypes = new Array('', 'ELEMENT (1)', 'ATTRIBUTE (2)','TEXT (3)', 'CDATA SECTION (4)', 'ENTITY REFERENCE (5)','ENTITY (6)', 'PROCESSING INSTRUCTION (7)', 'COMMENT (8)','DOCUMENT (9)', 'DOCUMENT TYPE (10)', 'DOCUMENT FRAGMENT (11)','NOTATION (12)');
//函数名:renderChildNodes
//参数:nodNode -- xml 节点
// intLevel -- 层数,用于缩进
function renderChildNodes(nodNode, intLevel)
{
var strNodes = ''; //用于显示的 html 字符串,处理的结果
var intCount = 0; //循环遍历用的临时变量
var intNode = 0; //循环遍历用的临时变量
var nodAttrList; //属性集