

 |
|
浏览整个XML文件
|
|
|
|
| 作者:未知 | 在接下来的练习中,你将建立一份HTML 网页,内含可以浏览代表整个XML 文件节点的DOM阶层架构的script,由根Document 元素开始。对于每个节点,script 会显示节点的名称、形态和节点值。Script 程序会将每节点的信息区块缩排,依照节点在阶层结构中的层级来排列。你可以使用此网页来显示代表任何XML 文件的节点,和学习到更多有关DOM 如何以节点来将不同形态的XML 文件与文件组件结构化。
建立浏览节点的网页
1. 在文字编辑器中, 开启 一份新的、空白的文本文件,然后输入显示于列表9-9 中的HTML 网页内容。(你可以在随书光盘的 ShowNodes.htm 档案中找到。) 2. 利用文字编辑器的 存盘 指令,将文件储存于本机硬盘中,文件名为 ShowNodes.htm : ShowNodes.htm <!--File Name:ShowNodes.htm --> <HTML> <HEAD> <TITLE>Show DOM Nodes</TITLE> <SCRIPT LANGUAGE="javascript" FOR="window" EVENT="ONLOAD"> /*get Document node:*/ Document =dsoXML.XMLDocument; /*start by passing the Document node to DisplayNodes:*/ DisplayDIV.innerText =DisplayNodes(Document,0); function DisplayNodes (Node,IndentLevel) { /*declare local variables for recursion:*/ var i; var DisplayString =""; /*build up the indentation for this level:*/ Indent =""; IndentDelta =" "; for (i=0;i <IndentLevel;++i) Indent +=IndentDelta; /*display the current node 's properties:*/ DisplayString +=Indent +"nodeName:" +Node.nodeName +"\n" +Indent +"nodeTypeType:" +Node.nodeType +"\n" +Indent +"nodeTypeString:" +Node.nodeTypeString +"\n" +Indent +"nodeValue:" +Node.nodeValue +"\n \n"; /*display each of the node 's attribute child nodes:*/ Indent +=IndentDelta; for (i=0; Node.attributes !=null &&i <Node.attributes.length; ++i) DisplayString +=Indent +"nodeName:" +Node.attributes(i).nodeName +"\n" +Indent +"nodeTypeType:" +Node.attributes(i).nodeType +"\n" +Indent +"nodeTypeString:" +Node.attributes(i).nodeTypeString +"\n" +Indent +"nodeValue:" +Node.attributes(i).nodeValue +"\n \n"; /*display each of the node 's nonattribute child nodes:*/ for (i=0;i <Node.childNodes.length;++i) DisplayString += DisplayNodes (Node.childNodes(i),IndentLevel +1); /*return the string containing the results:*/ return DisplayString; } </SCRIPT> </HEAD> <BODY> <XML ID="dsoXML " SRC=="Inventory Dom.xml"></XML> <H2>XML Document Object Model (DOM)Nodes</H2> <DIV ID="DisplayDIV"></DIV> </BODY> </HTML> | 第 1 2 页 |
|
|
上一篇:检查XML文件的有效性
下一篇:编写结构完整的XML文档
|
普克创业投资网刊载此文不代表同意其说法或描述,仅为提供更多信息。
|
|