XML DOM wrapper (COM)


SUBMITTED BY: Guest

DATE: May 30, 2013, 7:56 a.m.

FORMAT: Text only

SIZE: 80.3 kB

HITS: 1010

  1. #include-once
  2. ; =======================================================================
  3. ; Project Manager 3.0 Preprocessed - Date 25:04:2007 Time 14:28
  4. ; =======================================================================
  5. ; ------------------------------------------------------------------------------
  6. ;
  7. ; AutoIt Version: 3.0
  8. ; Language: English
  9. ; Description: Functions that assist with using xml files
  10. ; Stephen Podhajecki <gehossafats@netmdc.com>
  11. ; Dec 15, 2005, Update Jan10,2006, Update Feb 5,8,14-15, 2006
  12. ; Feb 24, 2006 Updated _XMLCreateCDATA, code cleaned up Gary Frost (custompcs@charter.net)
  13. ; Feb 24, 2006 bug fix in re-init COM error handler, rewrote _XMLCreateChildNodeWAttr()
  14. ; Jun 20, 2006 Added count to index[0] of the _XMLGetValue return value
  15. ; Jun 26, 2006 Changed _XMLCreateFile to include option flag for UTF-8 encoding
  16. ; Jun 29, 2006 Added count to index[0] of the _XMLGetValue return
  17. ; Changed _XMLFileOpen and _XMLFileCreate
  18. ; Mar 30, 2007 Rewrote _AddFormat function to break up tags( no indentation)
  19. ; Added _XMLTransform() which runs the document against a xsl(t) style sheet for indentation.
  20. ; Changed _XMLCreateRootChildWAttr() to use new formatting
  21. ; Changed _XMLChreateChildNode() to use new formatting
  22. ; Apr 02, 2007 Added _XMLReplaceChild()
  23. ; Apr 03, 2007 Changed other node creating function to use new formatting
  24. ; Changed _XMLFileOpen() _XMLFileCreate to take an optional version number of MSXML to use.
  25. ; Changed _XMLFileOpen() _XMLFileCreate find latest MSXML version logic.
  26. ; Apr 24, 2007 Fixed _XMLCreateChileNodeWAttr() - Instead of removal, It points to the function that replaced it.
  27. ; Apr 25, 2007 Added _XMLCreateAttrib()
  28. ; Fixed bug with _XMLCreateRootNodeWAttr , _XMLCreateChild[Node]WAttr() where an extra node with same name was added.
  29. ; Stripped extrenous comments.
  30. ; Removed dependency on Array.au3 (I added the func from Array.au3 and renamed it to avoid conflicts.)
  31. ; May 03, 2007 Changed method of msxml version lookup. Updated api call tips.
  32. ; May 11, 2007 Fixed missing \
  33. ; Jun 08, 2007 Fixed Namespace issue with _XMLCreateChildNode() and _XMLCreateChildNodeWAttr()
  34. ; Jun 12, 2007 Added workaround for MSXML6 to parse file with DTD's
  35. ; Jun 13, 2007 Fixed bug in _XMLGetField() where all text was returned in one node.
  36. ; Actually this is not a bug, because it is the way that WC3 says it will be returned
  37. ; However, it will now return in a way that is expected.
  38. ; _XMLGetValue now returns just the text associated with the node or empty string.
  39. ; Jul 20, 2007 Fixed bug where failure to open the xml file would return an empty xml object, it now returns 0(no object).
  40. ; Added object check to all applicable functions.
  41. ; Aug 08, 2007 Add a _XMLSetAutoSave() to turn off/on forced saving within each function. --Thanks drlava
  42. ; Added check for previous COM error handler. --Thanks Lukasz Suleja
  43. ; Aug 27,2007 Changed property setting order for _XMLFileOpen. The previous order was causing a problem with default namespaces.
  44. ; -- It seems that "selectionLanguage" needs to be declared before some other properties.
  45. ; Aug 31,2007 Fixed bug where _XMLUpdateField would inadvertantly erase child nodes.
  46. ; Sep 07,2007 Fixed _XMLDeleteNode bug where non-existant node cause COM error.
  47. ; Added _XMLNodeExist function to check for the existence of node or nodes matching the specified path
  48. ; Jan 05,2008 Fixed documentation problem in function header with _XMLGetAttrib.
  49. ; Feb 25,2008 Fixed dimensioning bug in _XMLGetChildren --Thanks oblique
  50. ; Mar 05,2008 Return values fixed for the following functions: --Thanks oblique
  51. ; _XMLFileOpen ,_XMLLoadXML,_XMLCreateFile,
  52. ; Documentation fixed for _XMLGetNodeCount,_XMLGetChildren --Thanks oblique
  53. ; Mar 07,2008 Small changes.
  54. ; Fixed an issue point out by lgr.
  55. ; ------------------------------------------------------------------------------
  56. ;===============================================================================
  57. ; XML DOM Wrapper functions
  58. ;
  59. ; These funtions require BETA as they need support for COM
  60. ;
  61. ;===============================================================================
  62. #cs defs to add to au3.api
  63. _XMLCreateFile($sPath, $sRootNode, [$bOverwrite = False]) Creates an XML file with the given name and root.(Requires: #include <_XMLDomWrapper.au3>)
  64. _XMLFileOpen($sXMLFile,[$sNamespace=""],[$ver=-1]) Creates an instance of an XML file.(Requires: #include <_XMLDomWrapper.au3>)
  65. ;==============================================================================
  66. _XMLGetChildNodes ( strXPath ) Selects XML child Node(s) of an element based on XPath input from root node. (Requires: #include <_XMLDomWrapper.au3>)
  67. _XMLGetNodeCount ( strXPath, strQry = "", iNodeType = 1 ) Get node count for specified path and type. (Requires: #include <_XMLDomWrapper.au3>)
  68. _XMLGetPath ( strXPath ) Returns a nodes full path based on XPath input from root node. (Requires: #include <_XMLDomWrapper.au3>)
  69. ;==============================================================================
  70. _XMLSelectNodes ( strXPath ) Selects XML Node(s) based on XPath input from root node. (Requires: #include <_XMLDomWrapper.au3>)
  71. _XMLGetField ( strXPath ) Get XML Field(s) based on XPath input from root node.(Requires: #include <_XMLDomWrapper.au3>)
  72. _XMLGetValue ( strXPath ) Get XML Field based on XPath input from root node. (Requires: #include <_XMLDomWrapper.au3>)
  73. _XMLGetChildText ( strXPath ) Selects XML child Node(s) of an element based on XPath input from root node. (Requires: #include <_XMLDomWrapper.au3>)
  74. _XMLUpdateField ( strXPath, strData ) Update existing node(s) based on XPath specs.(Requires: #include <_XMLDomWrapper.au3>)
  75. _XMLReplaceChild ( objOldNode, objNewNode, ns = "" ) Replaces a node with a new node. (Requires: #include <_XMLDomWrapper.au3>)
  76. ;==============================================================================
  77. _XMLDeleteNode ( strXPath ) Delete specified XPath node.(Requires: #include <_XMLDomWrapper.au3>)
  78. _XMLDeleteAttr ( strXPath, strAttrib ) Delete attribute for specified XPath(Requires: #include <_XMLDomWrapper.au3>)
  79. _XMLDeleteAttrNode ( strXPath, strAttrib ) Delete attribute node for specified XPath(Requires: #include <_XMLDomWrapper.au3>)
  80. ;==============================================================================
  81. _XMLGetAttrib ( strXPath, strAttrib, strQuery = "" ) Get XML attribute based on XPath input from root node.(Requires: #include <_XMLDomWrapper.au3>)
  82. _XMLGetAllAttrib ( strXPath, ByRef aName, ByRef aValue, strQry = "" ) Get all XML Field(s) attributes based on XPath input from root node.(Requires: #include <_XMLDomWrapper.au3>)
  83. _XMLGetAllAttribIndex ( strXPath, ByRef aName, ByRef aValue, strQry = "", NodeIndex = 0 ) Get all XML Field(s) attributes based on Xpathn and specific index.(Requires: #include <_XMLDomWrapper.au3>)
  84. _XMLSetAttrib ( strXPath, strAttrib, strValue = "" ) Set XML Field(s) attributes based on XPath input from root node.(Requires: #include <_XMLDomWrapper.au3>)
  85. ;==============================================================================
  86. _XMLCreateCDATA ( strNode, strCDATA, strNameSpc = "" ) Create a CDATA SECTION node directly under root. (Requires: #include <_XMLDomWrapper.au3>)
  87. _XMLCreateComment ( strNode, strComment ) Create a COMMENT node at specified path.(Requires: #include <_XMLDomWrapper.au3>)
  88. _XMLCreateAttrib ( strXPath,strAttrName,strAttrValue="" ) Creates an attribute for the specified node. (Requires: #include <_XMLDomWrapper.au3>)
  89. ;==============================================================================
  90. _XMLCreateRootChild ( strNode, strData = "", strNameSpc = "" ) Create node directly under root.(Requires: #include <_XMLDomWrapper.au3>)
  91. _XMLCreateRootNodeWAttr ( strNode, aAttr, aVal, strData = "", strNameSpc = "" ) Create a child node under root node with attributes.(Requires: #include <_XMLDomWrapper.au3>)
  92. _XMLCreateChildNode ( strXPath, strNode, strData = "", strNameSpc = "" ) Create a child node under the specified XPath Node.(Requires: #include <_XMLDomWrapper.au3>)
  93. _XMLCreateChildWAttr ( strXPath, strNode, aAttr, aVal, strData = "", strNameSpc = "" ) Create a child node under the specified XPath Node with Attributes. (Requires: #include <_XMLDomWrapper.au3>)
  94. ;==============================================================================
  95. _XMLSchemaValidate ( sXMLFile, ns, sXSDFile ) _XMLSchemaValidate($sXMLFile, $ns, $sXSDFile) Validate a document against a DTD. (Requires: #include <_XMLDomWrapper.au3>)
  96. _XMLGetDomVersion ( ) Returns the XSXML version currently in use. (Requires: #include <_XMLDomWrapper.au3>)
  97. _XMLError ( sError = "" ) Sets or Gets XML error message generated by XML functions.(Requires: #include <_XMLDomWrapper.au3>)
  98. _XMLUDFVersion ( ) eturns the UDF Version number. (Requires: #include <_XMLDomWrapper.au3>)
  99. _XMLTransform ( oXMLDoc, Style = "",szNewDoc="" ) Transfroms the document using built-in sheet or xsl file passed to function. (Requires: #include <_XMLDomWrapper.au3>)
  100. _XMLNodeExists( $strXPath) Checks for the existence of the specified path. (Requires: #include <_XMLDomWrapper.au3>)
  101. #ce
  102. ;===============================================================================
  103. ;Global variables
  104. Global Const $_XMLUDFVER = "1.0.3.87"
  105. Global Const $NODE_ELEMENT = 1
  106. Global Const $NODE_ATTRIBUTE = 2
  107. Global Const $NODE_TEXT = 3
  108. Global Const $NODE_CDATA_SECTION = 4
  109. Global Const $NODE_ENTITY_REFERENCE = 5
  110. Global Const $NODE_ENTITY = 6
  111. Global Const $NODE_PROCESSING_INSTRUCTION = 7
  112. Global Const $NODE_COMMENT = 8
  113. Global Const $NODE_DOCUMENT = 9
  114. Global Const $NODE_DOCUMENT_TYPE = 10
  115. Global Const $NODE_DOCUMENT_FRAGMENT = 11
  116. Global Const $NODE_NOTATION = 12
  117. Global $strFile
  118. Global $objDoc
  119. Global $oXMLMyError ;COM error handler OBJ ; Initialize SvenP 's error handler
  120. Global $sXML_error
  121. Global $debugging
  122. Global $DOMVERSION = -1
  123. Global $bXMLAUTOSAVE = True
  124. ;===============================================================================
  125. ;UDF functions
  126. ;===============================================================================
  127. ; Function Name: _XMLFileOpen
  128. ; Description: Creates an instance of an XML file.
  129. ; Parameter(s): $strXMLFile - the XML file to open
  130. ; $strNameSpc - the namespace to specifiy if the file uses one.
  131. ; $iVer - specifically try to use the version supplied here.
  132. ; $bValOnParse - validate the document as it is being parsed
  133. ; Syntax: _XMLFileOpen($strXMLFile, [$strNameSpc], [$iVer], [$bValOnParse] )
  134. ; Return Value(s): On Success - 1
  135. ; On Failure - -1 and set
  136. ; @Error to:
  137. ; 0 - No error
  138. ; 1 - Parse error, @Extended = MSXML reason
  139. ; 2 - No object
  140. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  141. ;===============================================================================
  142. Func _XMLFileOpen($strXMLFile, $strNameSpc = "", $iVer = -1, $bValOnParse = True)
  143. ;==== pick your poison
  144. If $iVer <> -1 Then
  145. If $iVer > -1 And $iVer < 7 Then
  146. $objDoc = ObjCreate("Msxml2.DOMDocument." & $iVer & ".0")
  147. If IsObj($objDoc) Then
  148. $DOMVERSION = $iVer
  149. EndIf
  150. Else
  151. MsgBox(266288, "Error:", "Failed to create object with MSXML version " & $iVer)
  152. SetError(1)
  153. Return 0
  154. EndIf
  155. Else
  156. For $x = 8 To 0 Step - 1
  157. If FileExists(@SystemDir & "\msxml" & $x & ".dll") Then
  158. $objDoc = ObjCreate("Msxml2.DOMDocument." & $x & ".0")
  159. If IsObj($objDoc) Then
  160. $DOMVERSION = $x
  161. ExitLoop
  162. EndIf
  163. EndIf
  164. Next
  165. EndIf
  166. If Not IsObj($objDoc) Then
  167. _XMLError("Error: MSXML not found. This object is required to use this program.")
  168. SetError(2)
  169. Return -1
  170. EndIf
  171. ;Thanks Lukasz Suleja
  172. $oXMLMyError = ObjEvent("AutoIt.Error")
  173. If $oXMLMyError = "" Then
  174. $oXMLMyError = ObjEvent("AutoIt.Error", "_XMLCOMEerr") ; ; Initialize SvenP 's error handler
  175. EndIf
  176. $strFile = $strXMLFile
  177. $objDoc.async = False
  178. $objDoc.preserveWhiteSpace = True
  179. $objDoc.validateOnParse = $bValOnParse
  180. if $DOMVERSION > 4 Then $objDoc.setProperty ("ProhibitDTD",false)
  181. $objDoc.Load ($strFile)
  182. $objDoc.setProperty ("SelectionLanguage", "XPath")
  183. $objDoc.setProperty ("SelectionNamespaces", $strNameSpc)
  184. if $objDoc.parseError.errorCode >0 Then consoleWrite($objDoc.parseError.reason&@LF)
  185. If $objDoc.parseError.errorCode <> 0 Then
  186. _XMLError("Error opening specified file: " & $strXMLFile & @CRLF & $objDoc.parseError.reason)
  187. ;Tom Hohmann 2008/02/29
  188. SetError(1,$objDoc.parseError.errorCode,-1)
  189. $objDoc = 0
  190. Return -1
  191. EndIf
  192. ;Tom Hohmann 2008/02/29
  193. Return 1
  194. EndFunc ;==>_XMLFileOpen
  195. ;===============================================================================
  196. ; Function Name: _XMLLoadXML
  197. ; Description: Creates an instance for a string of XML .
  198. ; Parameters: $strXML - The XML to load into the document
  199. ; Syntax: _XMLLoadXML($strXML)
  200. ; Return Value(s): On Success - 1
  201. ; On Failure - -1 and set @error to
  202. ; 1 - failed to create object, @Extended = MSXML reason
  203. ; 2 - no object found (MSXML required for _XML functions
  204. ;
  205. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>,Lukasz Suleja,Tom Hohmann
  206. ;===============================================================================
  207. Func _XMLLoadXML($strXML,$strNameSpc="", $iVer = -1, $bValOnParse = True)
  208. If $iVer <> -1 Then
  209. If $iVer > -1 And $iVer < 7 Then
  210. $objDoc = ObjCreate("Msxml2.DOMDocument." & $iVer & ".0")
  211. If IsObj($objDoc) Then
  212. $DOMVERSION = $iVer
  213. EndIf
  214. Else
  215. MsgBox(266288, "Error:", "Failed to create object with MSXML version " & $iVer)
  216. SetError(1)
  217. Return 0
  218. EndIf
  219. Else
  220. For $x = 8 To 0 Step - 1
  221. If FileExists(@SystemDir & "\msxml" & $x & ".dll") Then
  222. $objDoc = ObjCreate("Msxml2.DOMDocument." & $x & ".0")
  223. If IsObj($objDoc) Then
  224. $DOMVERSION = $x
  225. ExitLoop
  226. EndIf
  227. EndIf
  228. Next
  229. EndIf
  230. If Not IsObj($objDoc) Then
  231. _XMLError("Error: MSXML not found. This object is required to use this program.")
  232. SetError(2)
  233. Return -1
  234. EndIf
  235. ;Thanks Lukasz Suleja
  236. $oXMLMyError = ObjEvent("AutoIt.Error")
  237. If $oXMLMyError = "" Then
  238. $oXMLMyError = ObjEvent("AutoIt.Error", "_XMLCOMEerr") ; ; Initialize SvenP 's error handler
  239. EndIf
  240. $objDoc.async = False
  241. $objDoc.preserveWhiteSpace = True
  242. $objDoc.validateOnParse = $bValOnParse
  243. if $DOMVERSION > 4 Then $objDoc.setProperty ("ProhibitDTD",false)
  244. $objDoc.LoadXml ($strXML)
  245. $objDoc.setProperty ("SelectionLanguage", "XPath")
  246. $objDoc.setProperty ("SelectionNamespaces", $strNameSpc); "xmlns:ms='urn:schemas-microsoft-com:xslt'"
  247. If $objDoc.parseError.errorCode <> 0 Then
  248. _XMLError("Error loading the XML data: " & @CRLF & $objDoc.parseError.reason)
  249. ;Tom Hohmann 2008/02/29
  250. SetError(1,$objDoc.parseError.errorCode, -1)
  251. Return -1
  252. EndIf
  253. ;Tom Hohmann 2008/02/29
  254. Return 1
  255. EndFunc ;==>_MSXMLLoadXML
  256. ;===============================================================================
  257. ; Function Name: _XMLCreateFile
  258. ; Description: Create a new blank metafile with header.
  259. ; Parameter(s): $strPath - The xml filename with full path to create
  260. ; $strRoot - The root of the xml file to create
  261. ; $bOverwrite - boolean flag to auto overwrite existing file of same name.
  262. ; $bUTF8 - boolean flag to specify UTF-8 encoding in header.
  263. ; Syntax: _XMLCreateFile($strPath,$strRoot,[$bOverwrite],[$bUTF8])
  264. ; Return Value(s): On Success - 1
  265. ; On Failure - -1 and sets
  266. ; @Error to:
  267. ; 0 = No error
  268. ; 1 = Failed to create file
  269. ; 2 = No object
  270. ; 3 = File creation failed MSXML error
  271. ; 4 = File exists
  272. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  273. ;===============================================================================
  274. Func _XMLCreateFile($strPath, $strRoot, $bOverwrite = False, $bUTF8 = False, $ver = -1)
  275. Local $retval, $fe, $objPI, $objDoc, $rootElement
  276. $fe = FileExists($strPath)
  277. If $fe And Not $bOverwrite Then
  278. $retval = (MsgBox(4097, "File Exists:", "The specified file exits." & @CRLF & "Click OK to overwrite file or cancel to exit."))
  279. If $retval = 1 Then
  280. FileCopy($strPath, $strPath & @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & "-" & @MIN & "-" & @SEC & ".bak", 1)
  281. FileDelete($strPath)
  282. $fe = False
  283. Else
  284. _XMLError("Error failed to create file: " & $strPath & @CRLF & "File exists.")
  285. SetError(4)
  286. Return -1
  287. EndIf
  288. Else
  289. FileCopy($strPath, $strPath & ".old", 1)
  290. FileDelete($strPath)
  291. $fe = False
  292. EndIf
  293. If $fe = False Then
  294. If $ver <> -1 Then
  295. If $ver > -1 And $ver < 7 Then
  296. $objDoc = ObjCreate("Msxml2.DOMDocument." & $ver & ".0")
  297. If IsObj($objDoc) Then
  298. $DOMVERSION = $ver
  299. EndIf
  300. Else
  301. MsgBox(266288, "Error:", "Failed to create object with MSXML version " & $ver)
  302. SetError(3)
  303. Return 0
  304. EndIf
  305. Else
  306. For $x = 8 To 0 Step - 1
  307. If FileExists(@SystemDir & "\msxml" & $x & ".dll") Then
  308. $objDoc = ObjCreate("Msxml2.DOMDocument." & $x & ".0")
  309. If IsObj($objDoc) Then
  310. $DOMVERSION = $x
  311. ExitLoop
  312. EndIf
  313. EndIf
  314. Next
  315. EndIf
  316. If Not IsObj($objDoc) Then
  317. Return SetError(2)
  318. EndIf
  319. If $bUTF8 Then
  320. $objPI = $objDoc.createProcessingInstruction ("xml", "version=""1.0"" encoding=""UTF-8""")
  321. Else
  322. $objPI = $objDoc.createProcessingInstruction ("xml", "version=""1.0""")
  323. EndIf
  324. $objDoc.appendChild ($objPI)
  325. $rootElement = $objDoc.createElement ($strRoot)
  326. $objDoc.documentElement = $rootElement
  327. $objDoc.save ($strPath)
  328. If $objDoc.parseError.errorCode <> 0 Then
  329. _XMLError("Error Creating specified file: " & $strPath)
  330. ; Tom Hohmann 2008/02/29
  331. SetError(1, $objDoc.parseError.errorCode, -1)
  332. Return -1
  333. EndIf
  334. Return 1
  335. Else
  336. _XMLError("Error! Failed to create file: " & $strPath)
  337. SetError(1)
  338. Return 0
  339. EndIf
  340. Return 1
  341. EndFunc ;==>_XMLCreateFile
  342. ;===============================================================================
  343. ; Function Name: _XMLSelectNodes
  344. ; Description: Selects XML Node(s) based on XPath input from root node.
  345. ; Parameter(s): $strXPath - xml tree path from root node (root/child/child..)
  346. ; Syntax: _XMLSelectNode($strXPath)
  347. ; Return Value(s): On Success - An array of Nodes(count is in first element)
  348. ; On Failure - -1 and set @Error = 1
  349. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  350. ;===============================================================================
  351. Func _XMLSelectNodes($strXPath)
  352. If not IsObj($objDoc) then
  353. _XMLError("No object passed to function _XMLSelectNodes")
  354. Return SetError(2,0,-1)
  355. EndIf
  356. Local $objNode, $objNodeList, $arrResponse[1], $xmlerr
  357. $objNodeList = $objDoc.selectNodes ($strXPath)
  358. If Not IsObj($objNodeList) Then
  359. _XMLError("\nNo matching nodes found")
  360. Return SetError(1,0,-1)
  361. EndIf
  362. If $objNodeList.length < 1 Then
  363. _XMLError("\nNo matching nodes found")
  364. Return SetError(1,0,-1)
  365. EndIf
  366. For $objNode In $objNodeList
  367. _XMLArrayAdd($arrResponse, $objNode.nodeName)
  368. _DebugWrite($objNode.nodeName)
  369. _DebugWrite($objNode.namespaceURI)
  370. Next
  371. $arrResponse[0] = $objNodeList.length
  372. Return $arrResponse
  373. _XMLError("Error Selecting Node(s): " & $strXPath & $xmlerr)
  374. Return SetError(1,0,-1)
  375. EndFunc ;==>_XMLSelectNodes
  376. ;===============================================================================
  377. ; Function Name: _XMLGetField
  378. ; Description: Get XML Field(s) based on XPath input from root node.
  379. ; Parameter(s): $path xml tree path from root node (root/child/child..)
  380. ; Syntax: _XMLGetField($path)
  381. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  382. ; Return Value(s): On Success An array of fields text values(count is in first element)
  383. ; On Failure -1 set @Error = 1
  384. ;===============================================================================
  385. Func _XMLGetField($strXPath)
  386. If not IsObj($objDoc) then
  387. _XMLError("No object passed to function _XMLGetField")
  388. Return SetError(1,2,-1)
  389. EndIf
  390. Local $objNodeList, $arrResponse[1], $xmlerr, $szNodePath
  391. $objNodeList = $objDoc.selectSingleNode ($strXPath)
  392. If Not IsObj($objNodeList) Then
  393. _XMLError("\nNo Matching Nodes found")
  394. $arrResponse[0] = 0
  395. Return SetError(2,0,-1)
  396. EndIf
  397. If $objNodeList.hasChildNodes () Then
  398. Local $count = $objNodeList.childNodes.length
  399. For $x =1 to $count
  400. $objChild = $objNodeList.childNodes($x)
  401. _DebugWrite("ParentNode="&$objNodeList.parentNode.nodeType)
  402. If $objNodeList.parentNode.nodeType =$NODE_DOCUMENT Then
  403. $szNodePath="/"&$objNodeList.baseName &"/*["&$x&"]"
  404. Else
  405. $szNodePath = $objNodeList.baseName &"/*["&$x&"]"
  406. EndIf
  407. $aRet = _XMLGetValue($szNodePath)
  408. If IsArray($aRet) Then
  409. If UBound($aRet) > 1 Then
  410. _XMLArrayAdd($arrResponse, $aRet[1])
  411. _DebugWrite("GetField>Text:" & $aRet[1])
  412. EndIf
  413. Else
  414. _XMLArrayAdd($arrResponse, "")
  415. _DebugWrite("GetField>Text:" & "")
  416. EndIf
  417. Next
  418. $arrResponse[0] = UBound($arrResponse) - 1
  419. Return $arrResponse
  420. Else
  421. $arrResponse[0] = 0
  422. _XMLError("\nNo Child Nodes found")
  423. Return SetError(1,0,-1)
  424. EndIf
  425. _XMLError("Error Selecting Node(s): " & $strXPath & $xmlerr)
  426. Return SetError(1,0,-1)
  427. EndFunc ;==>_XMLGetField
  428. ;===============================================================================
  429. ; Function Name: _XMLGetValue
  430. ; Description: Get XML values based on XPath input from root node.
  431. ; Parameter(s): $strXPath - xml tree path from root node (root/child/child..)
  432. ; Syntax: _XMLGetValue($strXPath)
  433. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  434. ; Return Value(s): On Success An array of fields text values(count is in first element)
  435. ; On Failure -1 set
  436. ; @Error = 1
  437. ; @Extended to:
  438. ; 0 = No matching node
  439. ; 1 = No object passed
  440. ;===============================================================================
  441. Func _XMLGetValue($strXPath)
  442. If not IsObj($objDoc) then
  443. _XMLError("No object passed to function _XMLGetValue")
  444. Return SetError(1,1,-1)
  445. EndIf
  446. Local $objNodeList, $arrResponse[1], $objNodeChild, $xmlerr
  447. _DebugWrite("GetValue>$strXPath:"&$strXPath)
  448. $objNodeList = $objDoc.documentElement.selectNodes ($strXPath)
  449. If $objNodeList.length > 0 Then
  450. _DebugWrite("GetValue list length:" & $objNodeList.length)
  451. For $objNode In $objNodeList
  452. If $objNode.hasChildNodes () = False Then
  453. _XMLArrayAdd($arrResponse, $objNode.nodeValue)
  454. Else
  455. For $objNodeChild In $objNode.childNodes ()
  456. If $objNodeChild.nodeType = $NODE_CDATA_SECTION Then
  457. _XMLArrayAdd($arrResponse, $objNodeChild.data)
  458. _DebugWrite("GetValue>CData:" & $objNodeChild.data)
  459. ElseIf $objNodeChild.nodeType = $NODE_TEXT Then
  460. _XMLArrayAdd($arrResponse, $objNodeChild.Text)
  461. _DebugWrite("GetValue>Text:" & $objNodeChild.Text)
  462. EndIf
  463. Next
  464. EndIf
  465. Next
  466. $arrResponse[0] = UBound($arrResponse) - 1
  467. Return $arrResponse
  468. Else
  469. $xmlerr = @CRLF & "No matching node(s)found!"
  470. Return SetError(1,0,-1)
  471. EndIf
  472. _XMLError("Error Retrieving: " & $strXPath & $xmlerr)
  473. Return SetError(1,0, -1)
  474. EndFunc ;==>_XMLGetValue
  475. ;===============================================================================
  476. ; Function Name: _XMLDeleteNode
  477. ; Description: Deletes XML Node based on XPath input from root node.
  478. ; Parameter(s): $path xml tree path from root node (root/child/child..)
  479. ; Syntax: _XMLDeleteNode($path)
  480. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  481. ; Return Value(s): On Success 1
  482. ; On Failure -1 and Sets
  483. ; @Error to:
  484. ; 0 = No error
  485. ; 1 = Deletion error
  486. ; 2 = No object passed
  487. ;===============================================================================
  488. Func _XMLDeleteNode($strXPath)
  489. If not IsObj($objDoc) then
  490. _XMLError("No object passed to function _XMLDeleteNode")
  491. Return SetError(2,0,-1)
  492. EndIf
  493. Local $objNode, $xmlerr
  494. $objNode = $objDoc.selectNodes ($strXPath)
  495. If Not IsObj($objNode) Then $xmlerr = @CRLF & "Node Not found"
  496. if @error = 0 Then
  497. For $objChild in $objNode
  498. ConsoleWrite("Delete node " & $objChild.nodeName & @LF)
  499. $objChild.parentNode.removeChild ($objChild)
  500. Next
  501. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  502. Return 1
  503. EndIf
  504. _XMLError("Error Deleting Node: " & $strXPath & $xmlerr)
  505. Return SetError(1,0,-1)
  506. EndFunc ;==>_XMLDeleteNode
  507. ;===============================================================================
  508. ; Function Name: _XMLDeleteAttr
  509. ; Description: Delete XML Attribute based on XPath input from root node.
  510. ; Parameter(s): $strXPath xml tree path from root node (root/child/child..)
  511. ; $strAttribute The attribute node to delete
  512. ; Syntax: _XMLDeleteAttr($strPath,$strAttribute)
  513. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  514. ; Return Value(s): On Success 1
  515. ; On Failure -1 and sets
  516. ; @Error to:
  517. ; 0 = No error
  518. ; 1 = Error removing attribute
  519. ; 2 = No object
  520. ;===============================================================================
  521. Func _XMLDeleteAttr($strXPath, $strAttrib)
  522. If not IsObj($objDoc) then
  523. _XMLError("No object passed to function _XMLDeleteAttr")
  524. Return SetError(2,0,-1)
  525. EndIf
  526. Local $objNode, $objAttr, $xmlerr
  527. $objNode = $objDoc.selectSingleNode ($strXPath)
  528. If IsObj($objNode) Then
  529. $objAttr = $objNode.getAttributeNode ($strAttrib)
  530. If Not (IsObj($objAttr)) Then
  531. _XMLError("Attribute " & $strAttrib & " does not exist!")
  532. Return SetError(2,0,-1)
  533. EndIf
  534. $objAttr = $objNode.removeAttribute ($strAttrib)
  535. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  536. Return 1
  537. EndIf
  538. _XMLError("Error Removing Attribute: " & $strXPath & " - " & $strAttrib & @CRLF & $xmlerr)
  539. $xmlerr = ""
  540. Return SetError(1,0,-1)
  541. EndFunc ;==>_XMLDeleteAttr
  542. ;===============================================================================
  543. ; Function Name: _XMLDeleteAttrNode
  544. ; Description: Delete XML Attribute node based on XPath input from root node.
  545. ; Parameter(s): $strXpath xml tree path from root node (root/child/child..)
  546. ; $strAttribute The attribute node to delete
  547. ; Syntax: _XMLDeleteAttrNode($strXPath,$strAttribute)
  548. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  549. ; Return Value(s): On Success 1
  550. ; On Failure -1 and sets
  551. ; @Error to:
  552. ; 0 = No error
  553. ; 1 = Error removing node
  554. ; 2 = No object
  555. ;===============================================================================
  556. Func _XMLDeleteAttrNode($strXPath, $strAttrib)
  557. If not IsObj($objDoc) then
  558. _XMLError("No object passed to function _XMLDeleteAttrNode")
  559. Return SetError(2,0,-1)
  560. EndIf
  561. Local $objNode, $objAttr, $xmlerr
  562. $objNode = $objDoc.selectSingleNode ($strXPath)
  563. If Not IsObj($objNode) Then
  564. _XMLError("\nSpecified node not found!")
  565. Return SetError(2,0,-1)
  566. EndIf
  567. $objAttr = $objNode.removeAttributeNode ($objNode.getAttributeNode ($strAttrib))
  568. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  569. If Not (IsObj($objAttr)) Then
  570. _XMLError("\nUnspecified error:!")
  571. Return SetError(1,0,-1)
  572. EndIf
  573. Return 1
  574. EndFunc ;==>_XMLDeleteAttrNode
  575. ;===============================================================================
  576. ; Function Name: _XMLGetAttrib
  577. ; Description: Get XML Field based on XPath input from root node.
  578. ; Parameter(s): $path xml tree path from root node (root/child/child..)
  579. ; Syntax: _XMLGetAttrib($path,$attrib)
  580. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  581. ; Return Value(s): On Success The attribute value.
  582. ; On Failure -1 and sets
  583. ; @Error to:
  584. ; 0 = No error
  585. ; 1 = Attribute not found.
  586. ; 2 = No object
  587. ;===============================================================================
  588. Func _XMLGetAttrib($strXPath, $strAttrib, $strQuery = "")
  589. If not IsObj($objDoc) then
  590. _XMLError("No object passed to function _XMLGetAttrib")
  591. Return SetError(2,0,-1)
  592. EndIf
  593. ;Local $objNodeList, $arrResponse[1], $i, $xmlerr, $objAttr
  594. Local $objNodeList, $arrResponse, $i, $xmlerr, $objAttr
  595. $objNodeList = $objDoc.documentElement.selectNodes ($strXPath & $strQuery)
  596. _DebugWrite("Get Attrib length= " & $objNodeList.length)
  597. If $objNodeList.length > 0 Then
  598. For $i = 0 To $objNodeList.length - 1
  599. $objAttr = $objNodeList.item ($i).getAttribute ($strAttrib)
  600. $arrResponse = $objAttr
  601. _DebugWrite("RET>>" & $objAttr)
  602. Next
  603. Return $arrResponse
  604. EndIf
  605. $xmlerr = "\nNo qualified items found"
  606. _XMLError("Attribute " & $strAttrib & " not found for: " & $strXPath & $xmlerr)
  607. Return SetError(1,0,-1)
  608. EndFunc ;==>_XMLGetAttrib
  609. ;===============================================================================
  610. ; Function Name: _XMLSetAttrib
  611. ; Description: Set XML Field(s) based on XPath input from root node.
  612. ; Parameter(s): $path xml tree path from root node (root/child/child..)
  613. ; $attrib the attribute to set.
  614. ; $value the value to give the attribute defaults to ""
  615. ; Syntax: _XMLSetAttrib($path,$attrib,$value)
  616. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  617. ; Return Value(s) array of fields text values
  618. ; on error returns -1 and sets error to 1
  619. ;===============================================================================
  620. Func _XMLSetAttrib($strXPath, $strAttrib, $strValue = "", $iIndex =-1)
  621. If not IsObj($objDoc) then
  622. _XMLError("No object passed to function _XMLSetAttrib")
  623. Return SetError(1,8,-1)
  624. EndIf
  625. Local $objNodeList, $arrResponse[1], $i
  626. $objNodeList = $objDoc.selectNodes ($strXPath)
  627. _DebugWrite(" Node list Length: " & $objNodeList.length)
  628. If @error = 0 And $objNodeList.length > 0 Then
  629. If $iIndex > 0 Then
  630. $arrResponse[0] = $objNodeList.item ($iIndex).SetAttribute ($strAttrib, $strValue)
  631. Else
  632. ReDim $arrResponse[$objNodeList.length]
  633. For $i = 0 To $objNodeList.length - 1
  634. $arrResponse[$i] = $objNodeList.item ($i).SetAttribute ($strAttrib, $strValue)
  635. If $objDoc.parseError.errorCode <> 0 Then ExitLoop
  636. Next
  637. EndIf
  638. If $objDoc.parseError.errorCode <> 0 Then
  639. _XMLError("Error setting attribute for: " & $strXPath & @CRLF & $objDoc.parseError.reason)
  640. Return SetError(1,$objDoc.parseError.errorCode,-1)
  641. EndIf
  642. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  643. Return $arrResponse
  644. EndIf
  645. _XMLError("Error failed to set attribute for: " & $strXPath & @CRLF)
  646. SetError(1)
  647. Return -1
  648. EndFunc ;==>_XMLSetAttrib
  649. ;===============================================================================
  650. ; Function Name: _XMLGetAllAttrib
  651. ; Description: Get all XML Field(s) attributes based on XPath input from root node.
  652. ; Parameter(s): $path xml tree path from root node (root/child/child..)
  653. ; $query the query string in xml format
  654. ; $names the array to return the attrib names
  655. ; $value the array to return the attrib values
  656. ; [$query] DOM compliant query string (not really necessary as it becomes
  657. ;part of the path
  658. ; Syntax: _XMLGetAllAttrib($path,$query)
  659. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  660. ; Return Value(s) array of fields text values(number of items is in [0][0]
  661. ; on error set error to 1 and returns -1
  662. ;===============================================================================
  663. Func _XMLGetAllAttrib($strXPath, ByRef $aName, ByRef $aValue, $strQry = "")
  664. If not IsObj($objDoc) then
  665. _XMLError("No object passed to function _XMLGetAllAttrib")
  666. Return SetError(1,9,-1)
  667. EndIf
  668. Local $objNodeList, $objQueryNodes, $objNode, $arrResponse[2][1], $i
  669. $objQueryNodes = $objDoc.selectNodes ($strXPath & $strQry)
  670. If $objQueryNodes.length > 0 Then
  671. For $objNode In $objQueryNodes
  672. $objNodeList = $objNode.attributes
  673. If ($objNodeList.length) Then
  674. _DebugWrite("Get all attrib " & $objNodeList.length)
  675. ReDim $arrResponse[2][$objNodeList.length + 2]
  676. ReDim $aName[$objNodeList.length]
  677. ReDim $aValue[$objNodeList.length]
  678. For $i = 0 To $objNodeList.length - 1
  679. $arrResponse[0][$i + 1] = $objNodeList.item ($i).nodeName
  680. $arrResponse[1][$i + 1] = $objNodeList.item ($i).Value
  681. $aName[$i] = $objNodeList.item ($i).nodeName
  682. $aValue[$i] = $objNodeList.item ($i).Value
  683. Next
  684. Else
  685. _XMLError("No Attributes found for node")
  686. Return SetError(1,0, -1)
  687. EndIf
  688. Next
  689. $arrResponse[0][0] = $objNodeList.length
  690. Return $arrResponse
  691. EndIf
  692. _XMLError("Error retrieving attributes for: " & $strXPath & @CRLF)
  693. return SetError(1,0 ,-1)
  694. ; EndIf
  695. EndFunc ;==>_XMLGetAllAttrib
  696. ;===============================================================================
  697. ; Function Name: _XMLUpdateField
  698. ; Description: Update existing node(s) based on XPath specs.
  699. ; Parameter(s): $path Path from root node
  700. ; $new_data Data to update node with
  701. ; Syntax: _XMLUpdateField($path,$new_data)
  702. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  703. ; Return Value(s) on error set error to 1 and returns -1
  704. ;===============================================================================
  705. Func _XMLUpdateField($strXPath, $strData)
  706. If not IsObj($objDoc) then
  707. _XMLError("No object passed to function _XMLUpdateField")
  708. Return SetError(1,9,-1)
  709. EndIf
  710. Local $objField, $bUpdate, $objNode
  711. #forceref $objField
  712. $objField = $objDoc.selectSingleNode ($strXPath)
  713. If IsObj($objField) Then
  714. If $objField.hasChildNodes Then
  715. For $objChild In $objField.childNodes ()
  716. If $objChild.nodetype = $NODE_TEXT Then
  717. $objChild.Text = $strData
  718. $bUpdate = True
  719. ExitLoop
  720. EndIf
  721. Next
  722. EndIf
  723. if $bUpdate = False Then
  724. $objNode = $objDoc.createTextNode($strData)
  725. $objField.appendChild($objNode)
  726. EndIf
  727. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  728. $objField = ""
  729. Return
  730. EndIf
  731. _XMLError("Failed to update field for: " & $strXPath & @CRLF)
  732. Return SetError(1,0,-1)
  733. EndFunc ;==>_XMLUpdateField
  734. ;===============================================================================
  735. ; Function Name: _XMLCreateCDATA
  736. ; Description: Create a CDATA SECTION node directly under root.
  737. ; Parameter(s): $node name of node to create
  738. ; $data CDATA value
  739. ; Syntax: _XMLCreateCDATA($node,$data)
  740. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  741. ; Return Value(s):on error set error to 1 and returns -1
  742. ; fixme, won't append to exisiting node. must create new node.
  743. ;===============================================================================
  744. Func _XMLCreateCDATA($strNode, $strCDATA, $strNameSpc = "")
  745. If not IsObj($objDoc) then
  746. _XMLError("No object passed to function _XMLCreateCDATA")
  747. Return SetError(1,10,-1)
  748. EndIf
  749. Local $objChild, $objNode
  750. $objNode = $objDoc.createNode ($NODE_ELEMENT, $strNode, $strNameSpc)
  751. If IsObj($objNode)Then
  752. If Not ($objNode.hasChildNodes ()) Then
  753. _AddFormat($objDoc, $objNode)
  754. EndIf
  755. $objChild = $objDoc.createCDATASection ($strCDATA)
  756. $objNode.appendChild ($objChild)
  757. $objDoc.documentElement.appendChild ($objNode)
  758. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  759. _AddFormat($objDoc)
  760. $objChild = ""
  761. Return 1
  762. EndIf
  763. _XMLError("Failed to create CDATA Section: " & $strNode & @CRLF)
  764. Return SetError(1, 0 ,-1)
  765. EndFunc ;==>_XMLCreateCDATA
  766. ;===============================================================================
  767. ; Function Name: _XMLCreateComment
  768. ; Description: Create a COMMENT node at specified path.
  769. ; Parameter(s): $node name of node to create
  770. ; $comment the comment to add the to the xml file
  771. ; Syntax: _XMLCreateComment($node,$comment)
  772. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  773. ; Return Value(s) on error set error to 1 and returns -1
  774. ;===============================================================================
  775. Func _XMLCreateComment($strNode, $strComment)
  776. If not IsObj($objDoc) then
  777. _XMLError("No object passed to function _XMLCreateComment")
  778. Return SetError(1,11,-1)
  779. EndIf
  780. Local $objChild, $objNode
  781. $objNode = $objDoc.selectSingleNode ($strNode)
  782. If IsObj($objNode) Then
  783. If Not ($objNode.hasChildNodes ()) Then
  784. _AddFormat($objDoc, $objNode)
  785. EndIf
  786. $objChild = $objDoc.createComment ($strComment)
  787. $objNode.insertBefore ($objChild, $objNode.childNodes (0))
  788. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  789. _AddFormat($objDoc)
  790. $objChild = ""
  791. Return 1
  792. EndIf
  793. _XMLError("Failed to root child: " & $strNode & @CRLF)
  794. Return SetError(1,0, -1)
  795. EndFunc ;==>_XMLCreateComment
  796. ;===============================================================================
  797. ; Function Name: _XMLCreateAttribute
  798. ; Description:
  799. ; Parameter(s): $strXPath xml tree path from root node (root/child/child..)
  800. ; $strAttrName the attribute to set.
  801. ; $strAttrValue the value to give the attribute defaults to ""
  802. ; Syntax: _XMLCreateAttrib($strXPath,$strAttrName,$strAttrValue="")
  803. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  804. ; Return Value(s) 1 on success, 0 on error
  805. ;===============================================================================
  806. Func _XMLCreateAttrib($strXPath, $strAttrName, $strAttrValue = "")
  807. If not IsObj($objDoc) then
  808. _XMLError("No object passed to function _XMLCreateAttrib")
  809. Return SetError(1,12,-1)
  810. EndIf
  811. Local $objNode, $objAttr, $objAttrVal, $err
  812. $objNode = $objDoc.selectSingleNode ($strXPath)
  813. If IsObj($objNode) Then
  814. $objAttr = $objDoc.createAttribute ($strAttrName);, $strNameSpc)
  815. $objNode.SetAttribute ($strAttrName, $strAttrValue)
  816. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  817. $objAttr = 0
  818. $objAttrVal = 0
  819. $objNode = 0
  820. $err = $objDoc.parseError.errorCode
  821. If $err = 0 Then Return 1
  822. EndIf
  823. _XMLError("Error creating Attribute: " & $strAttrName & @CRLF & $strXPath & " does not exist." & @CRLF)
  824. Return 0
  825. EndFunc ;==>_XMLCreateAttrib
  826. ;===============================================================================
  827. ; Function Name: _XMLCreateRootChild
  828. ; Description: Create node directly under root.
  829. ; Parameter(s): $node name of node to create
  830. ; $value optional value to create
  831. ; Syntax: _XMLCreateRootChild($node,[$value])
  832. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  833. ; Return Value(s) on error set error to 1 and returns -1
  834. ;===============================================================================
  835. Func _XMLCreateRootChild($strNode, $strData = "", $strNameSpc = "")
  836. If not IsObj($objDoc) then
  837. _XMLError("No object passed to function _XMLCreateRootChild")
  838. Return SetError(1,14,-1)
  839. EndIf
  840. ConsoleWrite("_XMLCreateRootChild:"&$strNode&@LF)
  841. Local $objChild
  842. If Not ($objDoc.documentElement.hasChildNodes ()) Then
  843. _AddFormat($objDoc)
  844. EndIf
  845. $objChild = $objDoc.createNode ($NODE_ELEMENT, $strNode, $strNameSpc)
  846. If IsObj($objChild) Then
  847. If $strData <> "" Then $objChild.text = $strData
  848. $objDoc.documentElement.appendChild ($objChild)
  849. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  850. _AddFormat($objDoc)
  851. $objChild = 0
  852. Return 1
  853. EndIf
  854. _XMLError("Failed to root child: " & $strNode & @CRLF)
  855. Return SetError(1,0,-1)
  856. EndFunc ;==>_XMLCreateRootChild
  857. ;===============================================================================
  858. ; Function Name: _XMLCreateRootNodeWAttr
  859. ; Description: Create a child node under root node with attributes.
  860. ; Parameter(s): $node node to add with attibute(s)
  861. ; $[array]attrib attribute name(s) -- can be array
  862. ; $[array]value attribute value(s) -- can be array
  863. ; $data optional value to give the node.
  864. ; Requirements This function requires that each attribute name has
  865. ; a corresponding value.
  866. ; Syntax: _XMLCreateRootNodeWAttr($node,$array_attribs,$array_value)
  867. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  868. ; Return Value(s) on error set error to 1 or 2 and returns -1
  869. ;===============================================================================
  870. Func _XMLCreateRootNodeWAttr($strNode, $aAttr, $aVal, $strData = "", $strNameSpc = "")
  871. If not IsObj($objDoc) then
  872. _XMLError("No object passed to function _XMLCreateRootNodeWAttr")
  873. Return SetError(1,15,-1)
  874. EndIf
  875. Local $objChild, $objAttr, $objAttrVal
  876. $objChild = $objDoc.createNode ($NODE_ELEMENT, $strNode, $strNameSpc)
  877. If IsObj($objChild) Then
  878. If $strData <> "" Then $objChild.text = $strData
  879. If Not ($objDoc.documentElement.hasChildNodes ()) Then
  880. _AddFormat($objDoc)
  881. EndIf
  882. If IsArray($aAttr) And IsArray($aVal) Then
  883. If UBound($aAttr) <> UBound($aVal) Then
  884. _XMLError("Attribute and value mismatch" & @CRLF & "Please make sure each attribute has a matching value.")
  885. SetError(2)
  886. Return -1
  887. Else
  888. Local $i
  889. For $i = 0 To UBound($aAttr) - 1
  890. If $aAttr[$i] = "" Then
  891. _XMLError("Error creating child node: " & $strNode & @CRLF & " Attribute Name Cannot be NULL." & @CRLF)
  892. Return SetError(1,0,-1)
  893. EndIf
  894. $objAttr = $objDoc.createAttribute ($aAttr[$i]);, $strNameSpc)
  895. $objChild.SetAttribute ($aAttr[$i], $aVal[$i])
  896. Next
  897. EndIf
  898. Else
  899. $objAttr = $objDoc.createAttribute ($aAttr)
  900. $objChild.SetAttribute ($aAttr, $aVal)
  901. EndIf
  902. $objDoc.documentElement.appendChild ($objChild)
  903. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  904. _AddFormat($objDoc)
  905. $objChild = 0
  906. Return 1
  907. EndIf
  908. _XMLError("Failed to create root child with attributes: " & $strNode & @CRLF)
  909. Return SetError(1,0,-1)
  910. EndFunc ;==>_XMLCreateRootNodeWAttr
  911. ;===============================================================================
  912. ; Function Name: _XMLCreateChildNode
  913. ; Description: Create a child node under the specified XPath Node.
  914. ; Parameter(s): $path Path from root
  915. ; $node Node to add
  916. ; Syntax: _XMLCreateChildNode($path,$node)
  917. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  918. ; Return Value(s) on error set error to 1 and returns -1
  919. ;===============================================================================
  920. Func _XMLCreateChildNode($strXPath, $strNode, $strData = "", $strNameSpc = "")
  921. If not IsObj($objDoc) then
  922. _XMLError("No object passed to function _XMLCreateChildNode")
  923. Return SetError(1,16,-1)
  924. EndIf
  925. Local $objParent, $objChild, $objNodeList
  926. $objNodeList = $objDoc.selectNodes ($strXPath)
  927. If IsObj($objNodeList) And $objNodeList.length > 0 Then
  928. For $objParent In $objNodeList
  929. If Not ($objParent.hasChildNodes ()) Then
  930. _AddFormat($objDoc, $objParent)
  931. EndIf
  932. If $strNameSpc = "" Then
  933. If Not ($objParent.namespaceURI = 0 Or $objParent.namespaceURI = "") Then $strNameSpc = $objParent.namespaceURI
  934. EndIf
  935. ;ConsoleWrite("$strNameSpc=" & $strNameSpc & @LF)
  936. $objChild = $objDoc.createNode ($NODE_ELEMENT, $strNode, $strNameSpc)
  937. If $strData <> "" Then $objChild.text = $strData
  938. $objParent.appendChild ($objChild)
  939. _AddFormat($objDoc, $objParent)
  940. Next
  941. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  942. $objParent = ""
  943. $objChild = ""
  944. Return 1
  945. EndIf
  946. _XMLError("Error creating child node: " & $strNode & @CRLF & $strXPath & " does not exist." & @CRLF)
  947. Return SetError(1,0,-1)
  948. EndFunc ;==>_XMLCreateChildNode
  949. ;===============================================================================
  950. ; Function Name: _XMLCreateChildNodeWAttr
  951. ; Description: Create a child node(s) under the specified XPath Node with attributes.
  952. ; Parameter(s): $sPath Path from root
  953. ; $sNode node to add with attibute(s)
  954. ; $[array]attrib attribute name(s) -- can be array
  955. ; $[array]value attribute value(s) -- can be array
  956. ; $data Optional value to give the child node.
  957. ; Requirements This function requires that each attribute name has
  958. ; a corresponding value.
  959. ; Syntax: _XMLCreateChildNodeWAttr($path,$node,$[array]attrib,$]array]value)
  960. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  961. ; Return Value(s) 0 on error and set error 1 or 2
  962. ;===============================================================================
  963. Func _XMLCreateChildNodeWAttr($strXPath, $strNode, $aAttr, $aVal, $strData = "", $strNameSpc = "")
  964. Return _XMLCreateChildWAttr($strXPath, $strNode, $aAttr, $aVal, $strData, $strNameSpc)
  965. EndFunc ;==>_XMLCreateChildNodeWAttr
  966. ;===============================================================================
  967. ; Function Name: _XMLCreateChildWAttr
  968. ; Description: Create a child node(s) under the specified XPath Node with attributes.
  969. ; Parameter(s): $sPath Path from root
  970. ; $sNode node to add with attibute(s)
  971. ; $[array]attrib attribute name(s) -- can be array
  972. ; $[array]value attribute value(s) -- can be array
  973. ; $data Optional value to give the child node.
  974. ; Requirements This function requires that each attribute name has
  975. ; a corresponding value.
  976. ; Syntax: _XMLCreateChildWAttr($path,$node,$[array]attrib,$]array]value)
  977. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  978. ; Return Value(s) 0 on error and set error 1 or 2
  979. ;===============================================================================
  980. Func _XMLCreateChildWAttr($strXPath, $strNode, $aAttr, $aVal, $strData = "", $strNameSpc = "")
  981. If not IsObj($objDoc) then
  982. _XMLError("No object passed to function _XMLCreateChildWAttr")
  983. Return SetError(1,18,-1)
  984. EndIf
  985. Local $objParent, $objChild, $objAttr, $objAttrVal, $objNodeList
  986. $objNodeList = $objDoc.selectNodes ($strXPath)
  987. _DebugWrite("Node Selected")
  988. If IsObj($objNodeList) And $objNodeList.length <> 0 Then
  989. _DebugWrite("Entering if")
  990. For $objParent In $objNodeList
  991. If Not ($objParent.hasChildNodes ()) Then
  992. _AddFormat($objDoc, $objParent)
  993. EndIf
  994. _DebugWrite("Entering for")
  995. If $strNameSpc = "" Then
  996. If Not ($objParent.namespaceURI = 0 Or $objParent.namespaceURI = "") Then $strNameSpc = $objParent.namespaceURI
  997. EndIf
  998. $objChild = $objDoc.createNode ($NODE_ELEMENT, $strNode, $strNameSpc)
  999. If @error Then Return -1
  1000. If $strData <> "" Then $objChild.text = $strData
  1001. If IsArray($aAttr) And IsArray($aVal) Then
  1002. If UBound($aAttr) <> UBound($aVal) Then
  1003. _XMLError("Attribute and value mismatch" & @CRLF & "Please make sure each attribute has a matching value.")
  1004. Return SetError(2,0,-1)
  1005. Else
  1006. Local $i
  1007. For $i = 0 To UBound($aAttr) - 1
  1008. _DebugWrite("Entering inside for")
  1009. If $aAttr[$i] = "" Then
  1010. _XMLError("Error creating child node: " & $strNode & @CRLF & " Attribute Name Cannot be NULL." & @CRLF)
  1011. SetError(1)
  1012. Return -1
  1013. EndIf
  1014. _DebugWrite($aAttr[$i] & " " & $strNameSpc)
  1015. $objAttr = $objDoc.createAttribute ($aAttr[$i]);, $strNameSpc)
  1016. If @error Then ExitLoop
  1017. $objChild.SetAttribute ($aAttr[$i], $aVal[$i])
  1018. If @error <> 0 Then
  1019. _XMLError("Error creating child node: " & $strNode & @CRLF & $strXPath & " does not exist." & @CRLF)
  1020. Return SetError(1,0,-1)
  1021. EndIf
  1022. _DebugWrite("Looping inside for")
  1023. Next
  1024. EndIf
  1025. Else
  1026. If IsArray($aAttr) Or IsArray($aVal) Then
  1027. _XMLError("Type non-Array and Array detected" & @LF)
  1028. Return SetError(1,0,-1)
  1029. EndIf
  1030. If $aAttr = "" Then
  1031. _XMLError("Attribute Name cannot be empty string." & @LF)
  1032. Return SetError(5,0,-1)
  1033. EndIf
  1034. _DebugWrite($aAttr & " " & $strNameSpc)
  1035. $objAttr = $objDoc.createAttribute ($aAttr);, $strNameSpc)
  1036. $objChild.SetAttribute ($aAttr, $aVal)
  1037. EndIf
  1038. $objParent.appendChild ($objChild)
  1039. _DebugWrite("Looping for")
  1040. Next
  1041. _AddFormat($objDoc, $objParent)
  1042. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  1043. _DebugWrite("Saved")
  1044. $objParent = ""
  1045. $objChild = ""
  1046. _DebugWrite("Returning")
  1047. Return
  1048. EndIf
  1049. _XMLError("Error creating child node: " & $strNode & @CRLF & $strXPath & " does not exist." & @CRLF)
  1050. Return SetError(1,0,-1)
  1051. EndFunc ;==>_XMLCreateChildWAttr
  1052. ;===============================================================================
  1053. ; Function Name: _XMLGetChildText
  1054. ; Description: Selects XML child Node(s) of an element based on XPath input from root node.
  1055. ; Parameter(s): $path xml tree path from root node (root/child/child..)
  1056. ; Syntax: _XMLGetChildText($path)
  1057. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  1058. ; Return Value(s) array of Nodes or -1 on failure
  1059. ;===============================================================================
  1060. Func _XMLGetChildText($strXPath)
  1061. If not IsObj($objDoc) then
  1062. _XMLError("No object passed to function _XMLGetChildText")
  1063. Return SetError(1,19,-1)
  1064. EndIf
  1065. Local $objNodeList, $arrResponse[1], $xmlerr
  1066. $objNodeList = $objDoc.selectSingleNode ($strXPath)
  1067. If Not IsObj($objNodeList) Then
  1068. _XMLError(@CRLF & "No Matching Nodes found")
  1069. $arrResponse[0] = 0
  1070. Return SetError(1,0,-1)
  1071. EndIf
  1072. If $objNodeList.hasChildNodes () Then
  1073. For $objChild In $objNodeList.childNodes ()
  1074. If $objChild.nodeType = $NODE_ELEMENT Then
  1075. _XMLArrayAdd($arrResponse, $objChild.baseName)
  1076. ElseIf $objChild.nodeType = $NODE_TEXT Then
  1077. _XMLArrayAdd($arrResponse, $objChild.text)
  1078. EndIf
  1079. Next
  1080. $arrResponse[0] = UBound($arrResponse) - 1
  1081. Return $arrResponse
  1082. EndIf
  1083. $arrResponse[0] = 0
  1084. $xmlerr = @CRLF & "No Child Text Nodes found"
  1085. _XMLError("Error Selecting Node(s): " & $strXPath & $xmlerr)
  1086. Return SetError(1,0,-1)
  1087. EndFunc ;==>_XMLGetChildText
  1088. ;===============================================================================
  1089. ; Function Name: _XMLGetChildNodes
  1090. ; Description: Selects XML child Node(s) of an element based on XPath input from root node.
  1091. ; Parameter(s): $path xml tree path from root node (root/child/child..)
  1092. ; Syntax: _XMLGetChildNodes($path)
  1093. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  1094. ; Return Value(s) array of Nodes or -1 on failure
  1095. ;===============================================================================
  1096. Func _XMLGetChildNodes($strXPath)
  1097. If not IsObj($objDoc) then
  1098. _XMLError("No object passed to function _XMLGetChildNodes")
  1099. Return SetError(1,20,-1)
  1100. EndIf
  1101. Local $objNodeList, $arrResponse[1], $xmlerr
  1102. $objNodeList = $objDoc.selectSingleNode ($strXPath)
  1103. If Not IsObj($objNodeList) Then
  1104. _XMLError(@LF & "No Matching Nodes found")
  1105. $arrResponse[0] = 0
  1106. Return SetError(1,0,-1)
  1107. EndIf
  1108. If $objNodeList.hasChildNodes () Then
  1109. For $objChild In $objNodeList.childNodes ()
  1110. If $objChild.nodeType () = $NODE_ELEMENT Then
  1111. _DebugWrite($objChild.NamespaceURI &"::"& $objChild.baseName &@LF)
  1112. _XMLArrayAdd($arrResponse, $objChild.baseName)
  1113. EndIf
  1114. Next
  1115. $arrResponse[0] = UBound($arrResponse) - 1
  1116. Return $arrResponse
  1117. EndIf
  1118. $arrResponse[0] = 0
  1119. $xmlerr = @LF & "No Child Nodes found"
  1120. _XMLError("Error Selecting Node(s): " & $strXPath & $xmlerr)
  1121. Return SetError(1,0,-1)
  1122. EndFunc ;==>_XMLGetChildNodes
  1123. ;===============================================================================
  1124. ; Function Name: _XMLGetChildren
  1125. ; Description: Selects XML child Node(s) of an element based on XPath input from root node.
  1126. ; And returns there text values.
  1127. ; Parameter(s): $path xml tree path from root node (root/child/child..)
  1128. ; Syntax: _XMLGetChildren($path)
  1129. ; Return Value(s): On Success an array where
  1130. ; $array[0][0] = Size of array
  1131. ; $array[1][0] = Name
  1132. ; $array[1][1] = Text
  1133. ; $array[1][2] = NameSpaceURI
  1134. ; ...
  1135. ; $array[n][0] = Name
  1136. ; $array[n][1] = Text
  1137. ; $array[n][2] = NamespaceURI
  1138. ; On Failure - Returns -1 and sets @ERROR to
  1139. ; 1 - Failure
  1140. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  1141. ;===============================================================================
  1142. Func _XMLGetChildren($strXPath)
  1143. If not IsObj($objDoc) then
  1144. _XMLError("No object passed to function _XMLGetChildren")
  1145. Return SetError(1,21,-1)
  1146. EndIf
  1147. Local $objNodeList, $arrResponse[1][3], $xmlerr
  1148. $objNodeList = $objDoc.selectSingleNode ($strXPath)
  1149. If Not IsObj($objNodeList) Then
  1150. _XMLError(@LF & "No Matching Nodes found")
  1151. $arrResponse[0][0]= 0
  1152. Return SetError(1,0,-1)
  1153. EndIf
  1154. If $objNodeList.hasChildNodes () Then
  1155. For $objChild In $objNodeList.childNodes ()
  1156. If $objChild.nodeType () = $NODE_ELEMENT Then
  1157. Local $dims = UBound($arrResponse,1)
  1158. ReDim $arrResponse[$dims+1][3]
  1159. $arrResponse[$dims][0] = $objChild.baseName
  1160. $arrResponse[$dims][1] = $objChild.text
  1161. $arrResponse[$dims][2] = $objChild.NamespaceURI
  1162. ;_XMLArrayAdd($arrResponse, $objChild.baseName)
  1163. EndIf
  1164. Next
  1165. $arrResponse[0][0] = UBound($arrResponse,1) - 1
  1166. Return $arrResponse
  1167. EndIf
  1168. $arrResponse[0][0] = 0
  1169. $xmlerr = @LF & "No Child Nodes found"
  1170. _XMLError("Error Selecting Node(s): " & $strXPath & $xmlerr)
  1171. Return SetError(1,0,-1)
  1172. EndFunc ;==>_XMLGetChildren
  1173. ;===============================================================================
  1174. ; Function Name: _XMLGetNodeCount
  1175. ; Description: Get Node Count based on XPath input from root node.
  1176. ; Parameter(s): $path xml tree path from root node (root/child/child..)
  1177. ; [$query] DOM compliant query string (not really necessary as it becomes part of the path
  1178. ; $iNodeType The type of node to count. (element, attrib, comment etc.)
  1179. ; Syntax: _XMLGetNodeCount($path,$query,$iNodeType)
  1180. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com> & DickB
  1181. ; Return Value(s):0 or Number of Nodes found
  1182. ; on error set error to 1 and returns -1
  1183. ;===============================================================================
  1184. Func _XMLGetNodeCount($strXPath, $strQry = "", $iNodeType = 1)
  1185. If not IsObj($objDoc) then
  1186. _XMLError("No object passed to function _XMLGetNodeCount")
  1187. Return SetError(1,22,-1)
  1188. EndIf
  1189. Local $objQueryNodes, $objNode, $nodeCount = 0, $errMsg
  1190. $objQueryNodes = $objDoc.selectNodes ($strXPath & $strQry)
  1191. If @error = 0 And $objQueryNodes.length > 0 Then
  1192. For $objNode In $objQueryNodes
  1193. If $objNode.nodeType = $iNodeType Then $nodeCount = $nodeCount + 1
  1194. Next
  1195. Return $nodeCount
  1196. Else
  1197. $errMsg = "No nodes of specified type found."
  1198. EndIf
  1199. _XMLError("Error retrieving node count for: " & $strXPath & @CRLF & $errMsg & @CRLF)
  1200. SetError(1)
  1201. Return -1
  1202. ; EndIf
  1203. EndFunc ;==>_XMLGetNodeCount
  1204. ;===============================================================================
  1205. ; Function Name: _XMLGetAllAttribIndex
  1206. ; Description: Get all XML Field(s) attributes based on Xpathn and specific index.
  1207. ; Parameter(s): $sXpath xml tree path from root node (root/child/child..)
  1208. ; $aNames the array to return the attrib names
  1209. ; $aValue the array to return the attrib values
  1210. ; [$sQuery] DOM compliant query string (not really necessary as it becomes
  1211. ; [$iNode] node index.
  1212. ;part of the path
  1213. ; Syntax: _XMLGetAllAttribIndex($path,$aNames,$aValues,[$sQuery="",$iNode=0]])
  1214. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  1215. ; Return Value(s) array of attrib node names, array of attrib values
  1216. ; on error set error to 1 and returns -1
  1217. ;===============================================================================
  1218. Func _XMLGetAllAttribIndex($strXPath, ByRef $aName, ByRef $aValue, $strQry = "", $NodeIndex = 0)
  1219. If not IsObj($objDoc) then
  1220. _XMLError("No object passed to function _XMLGetAllAttribIndex")
  1221. Return SetError(1,23,-1)
  1222. EndIf
  1223. Local $objNodeList, $objQueryNodes, $arrResponse[2][1], $i
  1224. $objQueryNodes = $objDoc.selectNodes ($strXPath & $strQry)
  1225. If $objQueryNodes.length > 0 Then
  1226. $objNodeList = $objQueryNodes.item($NodeIndex).attributes
  1227. _DebugWrite("GetAllAttribIndex " & $objNodeList.length)
  1228. ReDim $arrResponse[2][$objNodeList.length + 1]
  1229. ReDim $aName[$objNodeList.length]
  1230. ReDim $aValue[$objNodeList.length]
  1231. For $i = 0 To $objNodeList.length - 1
  1232. $arrResponse[0][$i] = $objNodeList.item ($i).nodeName
  1233. $arrResponse[1][$i] = $objNodeList.item ($i).Value
  1234. $aName[$i] = $objNodeList.item ($i).nodeName
  1235. $aValue[$i] = $objNodeList.item ($i).Value
  1236. Next
  1237. Return $arrResponse
  1238. EndIf
  1239. _XMLError("Error retrieving attributes for: " & $strXPath & @CRLF)
  1240. Return SetError(1,0,-1)
  1241. EndFunc ;==>_XMLGetAllAttribIndex
  1242. ;===============================================================================
  1243. ; Function Name: _XMLGetPath
  1244. ; Description: Return a nodes full path based on XPath input from root node.
  1245. ; Parameter(s): $path xml tree path from root node (root/child/child..)
  1246. ; Syntax: _XMLGetPath($path)
  1247. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  1248. ; Return Value(s): array of fields text values -1 on failure
  1249. ;===============================================================================
  1250. Func _XMLGetPath($strXPath)
  1251. If not IsObj($objDoc) then
  1252. _XMLError("No object passed to function _XMLGetPath")
  1253. Return SetError(1,24,-1)
  1254. EndIf
  1255. If $DOMVERSION < 4 Then
  1256. _XMLError("Error DOM Version: " & "MSXML Version 4 or greater required for this function")
  1257. Return SetError(1,0,-1)
  1258. EndIf
  1259. Local $objNodeList, $arrResponse[1], $objNodeChild, $xmlerr, $nodepath, $ns
  1260. $objNodeList = $objDoc.selectNodes ($strXPath)
  1261. If $objNodeList.length > 0 Then
  1262. _DebugWrite("GetPath list length:" & $objNodeList.length)
  1263. For $objNode In $objNodeList
  1264. Local $objNode1 = $objNode
  1265. $nodepath = ""
  1266. $nodepathtag = ""
  1267. If $objNode.nodeType <> $NODE_DOCUMENT Then
  1268. $ns = $objNode.namespaceURI ()
  1269. If $ns <> "" Then
  1270. $ns = StringRight($ns, StringLen($ns) - StringInStr($ns, "/", 0, -1)) & ":"
  1271. EndIf
  1272. if $ns =0 then $ns =""
  1273. $nodepath = "/" & $ns & $objNode.nodeName () & $nodepath
  1274. EndIf
  1275. Do
  1276. $objParent = $objNode1.parentNode ()
  1277. _DebugWrite("parent " & $objParent.nodeName () & @LF)
  1278. If $objParent.nodeType <> $NODE_DOCUMENT Then
  1279. $ns = $objParent.namespaceURI ()
  1280. If $ns <> "" Then
  1281. ;$ns = StringRight($ns, StringLen($ns) - StringInStr($ns, "/", 0, -1)) & ":"
  1282. $ns &=":"
  1283. EndIf
  1284. if $ns =0 then $ns= ""
  1285. $nodepath = "/" &$ns & $objParent.nodeName ()& $nodepath
  1286. $objNode1 = $objParent
  1287. Else
  1288. $objNode1 = 0
  1289. EndIf
  1290. $objParent = 0
  1291. Until (Not (IsObj($objNode1)))
  1292. _DebugWrite("Path node> " & $nodepath & @LF)
  1293. _XMLArrayAdd($arrResponse, $nodepath)
  1294. Next
  1295. $arrResponse[0] = UBound($arrResponse) - 1
  1296. Return $arrResponse
  1297. EndIf
  1298. $xmlerr = @CRLF & "No matching node(s)found!"
  1299. _XMLError("Error Retrieving: " & $strXPath & $xmlerr)
  1300. Return SetError(1,0,-1)
  1301. EndFunc ;==>_XMLGetPath
  1302. ;===============================================================================
  1303. ; Function Name : _XMLGetPathInternal
  1304. ; Description : Returns the path of a valid node object.
  1305. ; Parameter(s) : $objNode A valid node object
  1306. ; Requirement(s) :
  1307. ; Return Value(s) : A string path, an empty string and set error on fail.
  1308. ; User CallTip :
  1309. ; Author(s) : Stephen Podhajecki <gehossafats at netmdc.com/>
  1310. ; Note(s) :
  1311. ;===============================================================================
  1312. Func _XMLGetPathInternal($objNode)
  1313. If not IsObj($objDoc) then
  1314. _XMLError("No object passed to function _XMLGetPathInternal")
  1315. Return SetError(1,25,-1)
  1316. EndIf
  1317. Local $nodepath, $na, $objParent
  1318. If IsObj($objNode) Then
  1319. $nodepath = "/" & $objNode.baseName
  1320. Do
  1321. $objParent = $objNode.parentNode ()
  1322. _DebugWrite("parent" & $objParent.nodeName () & ">" & @LF)
  1323. If $objParent.nodeType <> $NODE_DOCUMENT Then
  1324. $ns = $objParent.namespaceURI ()
  1325. If $ns = 0 Then $ns = ""
  1326. If $ns <> "" Then
  1327. $ns = StringRight($ns, StringLen($ns) - StringInStr($ns, "/", 0, -1)) & ":"
  1328. EndIf
  1329. $nodepath = "/" & $ns & $objParent.nodeName () & $nodepath
  1330. $objNode = $objParent
  1331. Else
  1332. $objNode = 0
  1333. EndIf
  1334. $objParent = 0
  1335. Until (Not (IsObj($objNode)))
  1336. _DebugWrite("Path node>" & $nodepath & @LF)
  1337. Return ($nodepath)
  1338. Else
  1339. Return SetError(1, 0, "")
  1340. EndIf
  1341. EndFunc ;==>_XMLGetPathInternal
  1342. ;===============================================================================
  1343. ; Function Name: _XMLReplaceChild
  1344. ; Description: Replaces a node with another
  1345. ; Parameter(s): $oldNode Node to replace
  1346. ; $newNode The replacement node.
  1347. ; Syntax: _XMLReplaceChild(oldNode,newNode)
  1348. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com> adapted from
  1349. ; http://www.perfectxml.com/msxmlAnswers.asp?Row_ID=65
  1350. ; Return Value(s)
  1351. ;===============================================================================
  1352. Func _XMLReplaceChild($objOldNode, $objNewNode, $ns = "")
  1353. If not IsObj($objDoc) then
  1354. _XMLError("No object passed to function _XMLReplaceChild")
  1355. Return SetError(1,26,-1)
  1356. EndIf
  1357. If $objOldNode = "" Or $objNewNode = "" Then Return SetError(1)
  1358. Local $nodeRoot
  1359. Local $nodeOld
  1360. Local $nodeNew
  1361. Local $nodeTemp
  1362. Local $bSuccess = False
  1363. ;No error handling done
  1364. With $objDoc
  1365. ;;.Load "c:\books.xml"
  1366. $nodeRoot = .documentElement
  1367. $oldNodes = $nodeRoot.selectNodes ($objOldNode)
  1368. ;'For each Node
  1369. For $nodeOld In $oldNodes
  1370. ;Create a New element
  1371. $nodeNew = .createNode ($NODE_ELEMENT, $objNewNode, $ns)
  1372. ;Copy attributes
  1373. For $nodeTemp In $nodeOld.Attributes
  1374. $nodeNew.Attributes.setNamedItem ($nodeTemp.cloneNode (True))
  1375. Next
  1376. ;Copy Child Nodes
  1377. For $nodeTemp In $nodeOld.childNodes
  1378. $nodeNew.appendChild ($nodeTemp)
  1379. Next
  1380. ;Replace with the renamed node
  1381. If IsObj($nodeOld.parentNode.replaceChild ($nodeNew, $nodeOld)) Then $bSuccess = 1
  1382. If Not ($objDoc.parseError.errorCode = 0) Then
  1383. _XMLError("_XMLReplaceChild:" & @LF & "Error Replacing Child: " & _
  1384. $objDoc.parseError.errorCode & _
  1385. " " & $objDoc.parseError.reason)
  1386. $bSucess = False
  1387. ExitLoop
  1388. Else
  1389. $bSuccess = True
  1390. EndIf
  1391. Next
  1392. .save ($strFile)
  1393. EndWith
  1394. $nodeRoot = 0
  1395. $nodeOld = 0
  1396. $nodeNew = 0
  1397. $nodeTemp = 0
  1398. Return $bSuccess
  1399. EndFunc ;==>_XMLReplaceChild
  1400. ;===============================================================================
  1401. ; Function Name: _XMLSchemaValidate
  1402. ; Description: Validates a document against a dtd.
  1403. ; Parameter(s): $sXMLFile The file to validate
  1404. ; $ns xml namespace
  1405. ; $sXSDFile DTD file to validate against.
  1406. ; Syntax: _XMLSchemaValidate($sXMLFile, $ns, $sXSDFile)
  1407. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  1408. ; Return Value(s) 1 on success or SetError(errorcode) on failure
  1409. ;===============================================================================
  1410. Func _XMLSchemaValidate($sXMLFile, $ns, $sXSDFile)
  1411. ;~ If not IsObj($objDoc) then
  1412. ;~ _XMLError("No object passed to function _XMLSchemaValidate")
  1413. ;~ Return SetError(1,27,-1)
  1414. ;~ EndIf
  1415. Local $cache, $xmldoc
  1416. $cache = ObjCreate("Msxml2.XMLSchemaCache." & $DOMVERSION & ".0")
  1417. If Not IsObj($cache) Then
  1418. MsgBox(266288, "XML Error", "Unable to instantiate the XML object" & @LF & "Please check your components.")
  1419. Return SetError(-1)
  1420. EndIf
  1421. $cache.add ($ns, $sXSDFile)
  1422. $xmldoc = ObjCreate("Msxml2.DOMDocument." & $DOMVERSION & ".0")
  1423. If Not IsObj($xmldoc) Then
  1424. MsgBox(266288, "XML Error", "Unable to instantiate the XML object" & @LF & "Please check your components.")
  1425. Return SetError(-1)
  1426. EndIf
  1427. $xmldoc.async = False
  1428. $xmldoc.schemas = $cache
  1429. $xmldoc.load ($sXMLFile)
  1430. If Not ($xmldoc.parseError.errorCode = 0) Then
  1431. _XMLError("_XMLSchemaValidate:" & @LF & "Error: " & $xmldoc.parseError.errorCode & " " & $xmldoc.parseError.reason)
  1432. Return SetError($xmldoc.parseError.errorCode)
  1433. EndIf
  1434. Return 0
  1435. EndFunc ;==>_XMLSchemaValidate
  1436. ;===============================================================================
  1437. ; Function Name: _XMLGetDomVersion
  1438. ; Description: Returns the version of msxml that is in use for the document.
  1439. ;
  1440. ; Syntax: _XMLGetDomVersion()
  1441. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  1442. ; Return Value(s) msxml version or -1
  1443. ;===============================================================================
  1444. Func _XMLGetDomVersion()
  1445. Return $DOMVERSION
  1446. EndFunc ;==>_XMLGetDomVersion
  1447. ;===============================================================================
  1448. ; Function Name: _XMLError
  1449. ; Description: Sets error message generated by XML functs.
  1450. ; or Gets the message that was Set.
  1451. ; Parameter(s): $sError Node from root to delete
  1452. ; Syntax: _XMLError([$sError)
  1453. ; Author(s): Stephen Podhajecki <gehossafats@netmdc.com>
  1454. ; Return Value(s) Nothing or Error message
  1455. ;===============================================================================
  1456. Func _XMLError($sError = "")
  1457. If $sError = "" Then
  1458. $sError = $sXML_error
  1459. $sXML_error = ""
  1460. Return $sError
  1461. Else
  1462. $sXML_error = StringFormat($sError)
  1463. EndIf
  1464. _DebugWrite($sXML_error)
  1465. EndFunc ;==>_XMLError
  1466. ;===============================================================================
  1467. ; Function Name: _XMLCOMEerr
  1468. ; Description: Displays a message box with the COM Error.
  1469. ; Parameter(s): none
  1470. ; Syntax: _XMLCOMEerr()
  1471. ; Author(s): SvenP 's error handler
  1472. ; Return Value(s)
  1473. ; From the forum this came.
  1474. ;===============================================================================
  1475. Func _XMLCOMEerr()
  1476. _ComErrorHandler()
  1477. Return
  1478. EndFunc ;==>_XMLCOMEerr
  1479. Func _ComErrorHandler($quiet = "")
  1480. Local $COMErr_Silent, $HexNumber
  1481. ;===============================================================================
  1482. ;added silent switch to allow the func returned to the option to display custom
  1483. ;error messages
  1484. If $quiet = True Or $quiet = False Then
  1485. $COMErr_Silent = $quiet
  1486. $quiet = ""
  1487. EndIf
  1488. ;===============================================================================
  1489. $HexNumber = Hex($oXMLMyError.number, 8)
  1490. If @error Then Return
  1491. Local $msg = "COM Error with DOM!" & @CRLF & @CRLF & _
  1492. "err.description is: " & @TAB & $oXMLMyError.description & @CRLF & _
  1493. "err.windescription:" & @TAB & $oXMLMyError.windescription & @CRLF & _
  1494. "err.number is: " & @TAB & $HexNumber & @CRLF & _
  1495. "err.lastdllerror is: " & @TAB & $oXMLMyError.lastdllerror & @CRLF & _
  1496. "err.scriptline is: " & @TAB & $oXMLMyError.scriptline & @CRLF & _
  1497. "err.source is: " & @TAB & $oXMLMyError.source & @CRLF & _
  1498. "err.helpfile is: " & @TAB & $oXMLMyError.helpfile & @CRLF & _
  1499. "err.helpcontext is: " & @TAB & $oXMLMyError.helpcontext
  1500. If $COMErr_Silent <> True Then
  1501. MsgBox(0, @AutoItExe, $msg)
  1502. Else
  1503. _XMLError($msg)
  1504. EndIf
  1505. SetError(1)
  1506. EndFunc ;==>_ComErrorHandler
  1507. ; simple helper functions
  1508. ;===============================================================================
  1509. ; Function Name: - _DebugWrite($message)
  1510. ; Description: - Writes a message to console with a crlf on the end
  1511. ; Parameter(s): - $message the message to display
  1512. ; Syntax: - _DebugWrite($message)
  1513. ; Author(s): -
  1514. ; Return Value(s) -
  1515. ;===============================================================================
  1516. Func _DebugWrite($message, $flag = @LF)
  1517. If $debugging Then
  1518. ConsoleWrite(StringFormat($message)&$flag)
  1519. EndIf
  1520. EndFunc ;==>_DebugWrite
  1521. ;===============================================================================
  1522. ; Function Name: _Notifier($Notifier_msg)
  1523. ; Description: displays a simple "ok" messagebox
  1524. ; Parameter(s): $Notifier_Msg The message to display
  1525. ; Syntax: _Notifier($Notifier_msg)
  1526. ; Author(s): -
  1527. ; Return Value(s) -
  1528. ;===============================================================================
  1529. Func _Notifier($Notifier_msg)
  1530. Return MsgBox(266288, @ScriptName, $Notifier_msg)
  1531. EndFunc ;==>_Notifier
  1532. ;===============================================================================
  1533. ; Function Name: - _SetDebug($flag =False)
  1534. ; Description: - Writes a message to console with a crlf on the end
  1535. ; Parameter(s): - $message the message to display
  1536. ; Syntax: - _DebugWrite($message)
  1537. ; Author(s): -
  1538. ; Return Value(s) -
  1539. ;===============================================================================
  1540. Func _SetDebug($debug_flag = True)
  1541. $debugging = $debug_flag
  1542. ConsoleWrite("Debug = " & $debugging & @LF)
  1543. EndFunc ;==>_SetDebug
  1544. ;===============================================================================
  1545. ; Function Name: _XMLUDFVersion()
  1546. ; Description: Returns UDF version number
  1547. ; Parameter(s): None
  1548. ; Syntax: _XMLUDFVersion()
  1549. ; Author(s): Stephen Podhajecki
  1550. ; Return Value(s) UDF version number
  1551. ;===============================================================================
  1552. Func _XMLUDFVersion()
  1553. Return $_XMLUDFVER
  1554. EndFunc ;==>_XMLUDFVersion
  1555. ;===============================================================================
  1556. ; Function Name: _XMLTransform
  1557. ; Description:
  1558. ; Parameter(s): $oXMLDoc The document to transform
  1559. ; $Style (optional) The stylesheet to use
  1560. ; $szNewDoc (optional) Save to this file.
  1561. ; Return Value(s): On Success returns 1
  1562. ; On Failure @Error = 1
  1563. ; User CallTip:
  1564. ; Author(s): Stephen Podhajecki <gehossafats at netmdc dot com>
  1565. ; Note(s):
  1566. ;===============================================================================
  1567. Func _XMLTransform($oXMLDoc="", $Style = "", $szNewDoc = "")
  1568. If $oXMLDoc = "" Then
  1569. $oXMLDoc = $objDoc
  1570. EndIf
  1571. If not IsObj($oXMLDoc) then
  1572. _XMLError("No object passed to function _XMLSetAttrib")
  1573. Return SetError(1,29,-1)
  1574. EndIf
  1575. Local $bIndented = False
  1576. Local $xslt = ObjCreate("MSXML2.XSLTemplate." & $DOMVERSION & ".0")
  1577. Local $xslDoc = ObjCreate("MSXML2.FreeThreadedDOMDocument." & $DOMVERSION & ".0")
  1578. Local $xmldoc = ObjCreate("MSXML2.DOMDocument." & $DOMVERSION & ".0")
  1579. Local $xslProc
  1580. $xslDoc.async = False
  1581. If FileExists($Style) Then
  1582. _DebugWrite("LoadXML:1:" & $xslDoc.load ($Style) & @LF)
  1583. Else
  1584. _DebugWrite("LoadXML:2:" & $xslDoc.loadXML (_GetDefaultStyleSheet()) & @LF)
  1585. EndIf
  1586. If $xslDoc.parseError.errorCode <> 0 Then
  1587. _XMLError("Error Transforming NodeToObject: " & $xslDoc.parseError.reason)
  1588. EndIf
  1589. If Not FileExists("XSLFile.xsl") Then FileWrite("XSLFile.xsl", $xslDoc.xml ())
  1590. $xslt.stylesheet = $xslDoc
  1591. $xslProc = $xslt.createProcessor ()
  1592. $xslProc.input = $objDoc
  1593. $oXMLDoc.transformNodeToObject ($xslDoc, $xmldoc)
  1594. If $oXMLDoc.parseError.errorCode <> 0 Then
  1595. _XMLError("_XMLTransform:" & @LF & "Error Transforming NodeToObject: " & $oXMLDoc.parseError.reason)
  1596. $bIndented = False
  1597. Else
  1598. $bIndented = True
  1599. EndIf
  1600. If $bIndented Then
  1601. If $szNewDoc <> "" Then
  1602. $xmldoc.save ($szNewDoc)
  1603. If $xmldoc.parseError.errorCode <> 0 Then
  1604. _XMLError("_XMLTransform:" & @LF & "Error Saving: " & $xmldoc.parseError.reason)
  1605. $bIndented = False
  1606. EndIf
  1607. Else
  1608. $xmldoc.save ($strFile)
  1609. $oXMLDoc.Load ($strFile)
  1610. If $oXMLDoc.parseError.errorCode <> 0 Then
  1611. _XMLError("_XMLTransform:" & @LF & "Error Saving: " & $oXMLDoc.parseError.reason)
  1612. $bIndented = False
  1613. EndIf
  1614. EndIf
  1615. EndIf
  1616. $xslProc = 0
  1617. $xslt = 0
  1618. $xslDoc = 0
  1619. $xmldoc = 0
  1620. Return $bIndented
  1621. EndFunc ;==>_XMLTransform
  1622. ;===============================================================================
  1623. ; Function Name: _GetDefaultStyleSheet
  1624. ; Description: Internal function, returns the default indenting style sheet
  1625. ; Parameter(s): Requirement(s):
  1626. ; Return Value(s): Stylesheet on success for nothing on failure.
  1627. ; User CallTip:
  1628. ; Author(s):
  1629. ; Note(s):
  1630. ;===============================================================================
  1631. Func _GetDefaultStyleSheet()
  1632. Return '<?xml version="1.0" encoding="ISO-8859-1"?>' & _
  1633. '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' & _
  1634. '<xsl:output method="xml" indent="yes"/> ' & _
  1635. '<xsl:template match="*">' & _
  1636. '<xsl:copy>' & _
  1637. '<xsl:copy-of select="@*" />' & _
  1638. '<xsl:apply-templates />' & _
  1639. '</xsl:copy>' & _
  1640. '</xsl:template>' & _
  1641. '<xsl:template match="comment()|processing-instruction()">' & _
  1642. '<xsl:copy />' & _
  1643. '</xsl:template>' & _
  1644. '</xsl:stylesheet>'
  1645. EndFunc ;==>_GetDefaultStyleSheet
  1646. ;===============================================================================
  1647. ; Function Name: _AddFormat
  1648. ; Description:
  1649. ; Parameter(s): $objDoc Document to format
  1650. ; $objParent Optional node to add formatting to
  1651. ; Requirement(s):
  1652. ; Return Value(s):
  1653. ; User CallTip:
  1654. ; Author(s): Stephen Podhajecki <gehossafats a t netmdc.com>
  1655. ; Note(s): just break up the tags, no indenting is done here.
  1656. ;===============================================================================
  1657. Func _AddFormat($objDoc, $objParent = "")
  1658. If not IsObj($objDoc) then
  1659. _XMLError("No object passed to function _XMLAddFormat")
  1660. Return SetError(1,30,-1)
  1661. EndIf
  1662. $objFormat = $objDoc.createTextNode (@CR)
  1663. If IsObj($objParent) Then
  1664. $objParent.appendChild ($objFormat)
  1665. Else
  1666. $objDoc.documentElement.appendChild ($objFormat)
  1667. EndIf
  1668. If ($bXMLAUTOSAVE = True) Then $objDoc.save ($strFile)
  1669. EndFunc ;==>_AddFormat
  1670. ;===============================================================================
  1671. ; Function Name: _XMLSetAutoSave
  1672. ; Description: Set the forced save to on or off
  1673. ; Parameter(s): $bSave
  1674. ; Requirement(s):
  1675. ; Return Value(s): previous state
  1676. ; User CallTip:
  1677. ; Author(s): Stephen Podhajecki <gehossafats a t netmdc.com>
  1678. ; Note(s): Defaults to true.
  1679. ;===============================================================================
  1680. Func _XMLSetAutoSave($bSave = True)
  1681. Local $oldSave = $bXMLAUTOSAVE
  1682. if $bSave = False Then
  1683. $bXMLAUTOSAVE = True
  1684. Else
  1685. $bXMLAUTOSAVE = False
  1686. EndIf
  1687. Return $oldSave
  1688. EndFunc
  1689. ;===============================================================================
  1690. ; Function Name: _XMLSaveDoc
  1691. ; Description: Save the current xml doc
  1692. ; Parameter(s): $sFile - The filename to save the xml doc as.
  1693. ; Requirement(s):
  1694. ; Return Value(s): none
  1695. ; User CallTip:
  1696. ; Author(s): Stephen Podhajecki <gehossafats a t netmdc.com>
  1697. ; Note(s): Defaults to the current filename.
  1698. ;===============================================================================
  1699. Func _XMLSaveDoc($sFile="")
  1700. if $sFile = "" Then $sFile = $strFile
  1701. $objDoc.save($sFile)
  1702. EndFunc
  1703. ;===============================================================================
  1704. ; Function Name: _XMLNodeExists
  1705. ; Description: Checks for the existence of a node or nodes matching the specified path
  1706. ; Parameter(s): $strXPath - Path to check for.
  1707. ; Requirement(s):
  1708. ; Return Value(s): 1 or Higher on success, 0 on failure
  1709. ; @Error set to
  1710. ; 0 no error
  1711. ; 1 No XML object @extended = 31
  1712. ; 2 Node not found
  1713. ; User CallTip:
  1714. ; Author(s): Stephen Podhajecki <gehossafats a t netmdc.com>
  1715. ; Note(s): Returns the number of nodes found (could be greater than 1)
  1716. ;===============================================================================
  1717. Func _XMLNodeExists($strXPath)
  1718. If not IsObj($objDoc) then
  1719. _XMLError("No object passed to function _XMLNodeExists")
  1720. Return SetError(1,31,0)
  1721. EndIf
  1722. Local $objNode, $iCount
  1723. Local $objNode = $objDoc.SelectNodes($strXPath)
  1724. If IsObj($objNode) Then $iCount = $objNode.length
  1725. $objNode = 0
  1726. if $iCount Then Return $iCount
  1727. Return SetError(2,0,0)
  1728. EndFunc
  1729. ; =======================================================================
  1730. ; Preprocessed included functions...
  1731. ; =======================================================================
  1732. Func _XMLArrayAdd(ByRef $avArray, $sValue)
  1733. If IsArray($avArray) Then
  1734. ReDim $avArray[UBound($avArray) + 1]
  1735. $avArray[UBound($avArray) - 1] = $sValue
  1736. SetError(0)
  1737. Return 1
  1738. Else
  1739. SetError(1)
  1740. Return 0
  1741. EndIf
  1742. EndFunc ;==>_XMLArrayAdd

comments powered by Disqus