Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
5.9 KiB

  1. Attribute VB_Name = "XML"
  2. Option Explicit
  3. Public Function XMLFindFirstNode( _
  4. ByVal i_DOMNode As MSXML2.IXMLDOMNode, _
  5. ByVal i_strName As String _
  6. ) As MSXML2.IXMLDOMNode
  7. Dim DOMNode As MSXML2.IXMLDOMNode
  8. Set XMLFindFirstNode = Nothing
  9. If (i_DOMNode.nodeName = i_strName) Then
  10. Set XMLFindFirstNode = i_DOMNode
  11. Exit Function
  12. ElseIf (Not (i_DOMNode.firstChild Is Nothing)) Then
  13. For Each DOMNode In i_DOMNode.childNodes
  14. Set XMLFindFirstNode = XMLFindFirstNode(DOMNode, i_strName)
  15. If (Not (XMLFindFirstNode Is Nothing)) Then
  16. Exit Function
  17. End If
  18. Next
  19. End If
  20. End Function
  21. Public Function XMLGetAttribute( _
  22. ByVal i_DOMNode As MSXML2.IXMLDOMNode, _
  23. ByVal i_strAttributeName As String _
  24. ) As String
  25. On Error Resume Next
  26. XMLGetAttribute = i_DOMNode.Attributes.getNamedItem(i_strAttributeName).nodeValue
  27. End Function
  28. Public Sub XMLSetAttribute( _
  29. ByVal u_DOMNode As MSXML2.IXMLDOMNode, _
  30. ByVal i_strAttributeName As String, _
  31. ByVal i_strAttributeValue As String _
  32. )
  33. Dim Element As MSXML2.IXMLDOMElement
  34. Set Element = u_DOMNode
  35. Element.setAttribute i_strAttributeName, i_strAttributeValue
  36. End Sub
  37. Public Function XMLCreateChildElement( _
  38. ByVal u_DOMNodeParent As MSXML2.IXMLDOMNode, _
  39. ByVal i_strElementName As String, _
  40. ByVal i_blnNameValuePairsExist As Boolean, _
  41. ByRef i_arrNameValuePairs() As String _
  42. ) As MSXML2.IXMLDOMNode
  43. Dim DOMDoc As MSXML2.DOMDocument
  44. Dim Element As MSXML2.IXMLDOMElement
  45. Dim intIndex As Long
  46. Set DOMDoc = u_DOMNodeParent.ownerDocument
  47. Set Element = DOMDoc.createElement(i_strElementName)
  48. Set XMLCreateChildElement = u_DOMNodeParent.appendChild(Element)
  49. If (i_blnNameValuePairsExist) Then
  50. For intIndex = LBound(i_arrNameValuePairs) To UBound(i_arrNameValuePairs)
  51. Element.setAttribute i_arrNameValuePairs(intIndex, 0), _
  52. i_arrNameValuePairs(intIndex, 1)
  53. Next
  54. End If
  55. End Function
  56. Public Sub XMLCopyAttributes( _
  57. ByRef i_DOMNodeSrc As MSXML2.IXMLDOMNode, _
  58. ByRef u_DOMNodeDest As MSXML2.IXMLDOMNode _
  59. )
  60. Dim Attr As MSXML2.IXMLDOMAttribute
  61. Dim Element As MSXML2.IXMLDOMElement
  62. Set Element = u_DOMNodeDest
  63. For Each Attr In i_DOMNodeSrc.Attributes
  64. Element.setAttribute Attr.Name, Attr.Value
  65. Next
  66. End Sub
  67. Public Function XMLCopyDOMTree( _
  68. ByRef i_DOMNodeSrc As MSXML2.IXMLDOMNode, _
  69. ByRef u_DOMNodeParent As MSXML2.IXMLDOMNode _
  70. ) As MSXML2.IXMLDOMNode
  71. Dim DOMNode As MSXML2.IXMLDOMNode
  72. Dim DOMElement As MSXML2.IXMLDOMElement
  73. Dim DOMText As MSXML2.IXMLDOMText
  74. Dim DOMAttr As MSXML2.IXMLDOMAttribute
  75. Dim DOMNodeChild As MSXML2.IXMLDOMNode
  76. If (i_DOMNodeSrc.ownerDocument Is u_DOMNodeParent.ownerDocument) Then
  77. Set DOMNode = i_DOMNodeSrc.cloneNode(True)
  78. u_DOMNodeParent.appendChild DOMNode
  79. Else
  80. ' Different DOM Nodes, so we really have to copy and
  81. ' recreate the node from one DOM Tree to another.
  82. Select Case i_DOMNodeSrc.nodeType
  83. Case NODE_TEXT
  84. Set DOMText = u_DOMNodeParent.ownerDocument.createTextNode(i_DOMNodeSrc.Text)
  85. Set DOMNode = u_DOMNodeParent.appendChild(DOMText)
  86. Case Else
  87. Set DOMElement = u_DOMNodeParent.ownerDocument.createElement(i_DOMNodeSrc.nodeName)
  88. Set DOMNode = u_DOMNodeParent.appendChild(DOMElement)
  89. For Each DOMAttr In i_DOMNodeSrc.Attributes
  90. DOMElement.setAttribute DOMAttr.nodeName, DOMAttr.nodeValue
  91. Next
  92. For Each DOMNodeChild In i_DOMNodeSrc.childNodes
  93. XMLCopyDOMTree DOMNodeChild, DOMNode
  94. Next
  95. End Select
  96. End If
  97. Set XMLCopyDOMTree = DOMNode
  98. End Function
  99. Private Function p_XMLValidChar( _
  100. ByRef i_char As String _
  101. ) As Boolean
  102. Dim intAscW As Long
  103. intAscW = AscW(i_char)
  104. ' Sometimes AscW returns a negative number. Eg 0x8021 -> 0xFFFF8021
  105. intAscW = intAscW And &HFFFF&
  106. Select Case intAscW
  107. Case &H9&, &HA&, &HD&, &H20& To &HD7FF&, &HE000& To &HFFFD&
  108. p_XMLValidChar = True
  109. Case Else
  110. p_XMLValidChar = False
  111. End Select
  112. End Function
  113. Public Function XMLValidString( _
  114. ByRef i_str As String _
  115. ) As Boolean
  116. Dim intIndex As Long
  117. Dim intLength As Long
  118. intLength = Len(i_str)
  119. For intIndex = 1 To intLength
  120. If (Not p_XMLValidChar(Mid$(i_str, intIndex, 1))) Then
  121. XMLValidString = False
  122. Exit Function
  123. End If
  124. Next
  125. XMLValidString = True
  126. End Function
  127. Public Function XMLMakeValidString( _
  128. ByRef i_str As String _
  129. ) As String
  130. Dim intIndex As Long
  131. Dim intLength As Long
  132. Dim str As String
  133. XMLMakeValidString = i_str
  134. intLength = Len(i_str)
  135. For intIndex = 1 To intLength
  136. If (Not p_XMLValidChar(Mid$(i_str, intIndex, 1))) Then
  137. XMLMakeValidString = Mid$(XMLMakeValidString, 1, intIndex - 1) & " " & _
  138. Mid$(XMLMakeValidString, intIndex + 1)
  139. End If
  140. Next
  141. End Function
  142. Public Function XMLEscape( _
  143. ByVal i_str As String _
  144. ) As String
  145. XMLEscape = XMLMakeValidString(i_str)
  146. XMLEscape = Replace$(XMLEscape, "&", "&")
  147. XMLEscape = Replace$(XMLEscape, "<", "&lt;")
  148. XMLEscape = Replace$(XMLEscape, ">", "&gt;")
  149. XMLEscape = Replace$(XMLEscape, "'", "&apos;")
  150. XMLEscape = Replace$(XMLEscape, """", "&quot;")
  151. End Function
  152. Public Function XMLUnEscape( _
  153. ByVal i_str As String _
  154. ) As String
  155. XMLUnEscape = i_str
  156. XMLUnEscape = Replace$(XMLUnEscape, "&quot;", """")
  157. XMLUnEscape = Replace$(XMLUnEscape, "&apos;", "'")
  158. XMLUnEscape = Replace$(XMLUnEscape, "&gt;", ">")
  159. XMLUnEscape = Replace$(XMLUnEscape, "&lt;", "<")
  160. XMLUnEscape = Replace$(XMLUnEscape, "&amp;", "&")
  161. End Function