📄 xml-via-dom.aspx
字号:
<%@Page Language="VB" %>
<%@Import Namespace="System.XML" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Accessing XML documents using the DOM</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Accessing XML documents using the DOM</span><hr />
<!--------------------------------------------------------------------------->
<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server"> </div>
<div id="outResults" runat="server"></div>
<script language="vb" runat="server">
Sub Page_Load()
'create physical path to booklist.xml sample file (in same folder as ASPX page)
Dim strCurrentPath As String = Request.PhysicalPath
Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "booklist.xml"
'create a new XMLDocument object
Dim objXMLDoc As New XMLDocument()
Try
'load the XML file into the XMLDocument object
objXMLDoc.Load(strXMLPath)
outDocURL.innerHTML = "Loaded file: <b>" & strXMLPath & "</b>"
Catch objError As Exception
'display error details
outError.innerHTML = "<b>* Error while accessing document</b>.<br />" _
& objError.Message & "<br />" & objError.Source
Exit Sub ' and stop execution
End Try
'now ready to parse the XML document
'it must be well-formed to have loaded without error
'call a recursive function to iterate through all the nodes
'in the document creating a string that is placed in the <div> above
Dim strNodes As String
outResults.innerHTML = strNodes & GetChildNodes(objXMLDoc.ChildNodes, 0)
End Sub
Function GetChildNodes(objNodeList As XMLNodeList, intLevel As Integer) As String
Dim strNodes As String = ""
Dim objNode As XMLNode
Dim objAttr As XMLAttribute
'iterate through all the child nodes for the current node
For Each objNode In objNodeList
'display information about this node
strNodes = strNodes & GetIndent(intLevel) _
& GetNodeType(objNode.NodeType) & ": <b>" & objNode.Name
'if it is an XML Declaration node, display the 'special' properties
If objNode.NodeType = XMLNodeType.XmlDeclaration Then
'cast the XMLNode object to an XmlDeclaration object
Dim objXMLDec =CType(objNode, XmlDeclaration)
strNodes = strNodes & "</b> version=<b>" & objXMLDec.Version _
& "</b> standalone=<b>" & objXMLDec.Standalone & "</b><br />"
Else
'just display the generic 'value' property
strNodes = strNodes & "</b> value=<b>" & objNode.Value & "</b><br />"
End If
'if it is an Element node, iterate through the Attributes
'collection displaying information about each attribute
If objNode.NodeType = XMLNodeType.Element Then
'display the attribute information for each attribute
For Each objAttr In objNode.Attributes
strNodes = strNodes & GetIndent(intLevel + 1) _
& GetNodeType(objAttr.NodeType) & ": <b>" & objAttr.Name _
& "</b> value=<b>" & objAttr.Value & "</b><br />"
Next
End If
'if this node has child nodes, call the same function recursively
'to display the information for it and each of its child node
If objNode.HasChildNodes Then
strNodes = strNodes & GetChildNodes(objNode.childNodes, intLevel + 1)
End If
Next 'go to next node
Return strNodes 'pass the result back to the caller
End Function
Function GetIndent(intLevel As Integer)
'returns a string of non-breaking spaces used to indent each line
Dim strIndent As String = ""
Dim intIndent As Integer
For intIndent = 0 To intLevel
strIndent = strIndent & " "
Next
Return strIndent
End Function
Function GetNodeType(intType As Integer) As String
'returns the node type as a string
Select Case (intType)
Case 0: Return "NONE"
Case 1: Return "ELEMENT"
Case 2: Return "ATTRIBUTE"
Case 3: Return "TEXT"
Case 4: Return "CDATA SECTION"
Case 5: Return "ENTITY REFERENCE"
Case 6: Return "ENTITY"
Case 7: Return "PROCESSING INSTRUCTION"
Case 8: Return "COMMENT"
Case 9: Return "DOCUMENT"
Case 10: Return "DOCUMENT TYPE"
Case 11: Return "DOCUMENT FRAGMENT"
Case 12: Return "NOTATION"
Case 13: Return "WHITESPACE"
Case 14: Return "SIGNIFICANT WHITESPACE"
Case 15: Return "END ELEMENT"
Case 16: Return "END ENTITY"
Case 17: Return "XML DECLARATION"
Case 18: Return "NODE (ALL)"
Case Else: Return "UNKNOWN"
End Select
End Function
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -