AutoIT‎ > ‎

XML Parsing in AutoIT using Msxml2.DOMDocument

posted Feb 25, 2014, 1:33 PM by Chris G   [ updated Mar 20, 2014, 11:29 AM ]
AutoIT Code Sample - Parsing an XML document. Here is the sample XML document to parse:

<results>
<status>OK</status>
<url>
http://technotes.videreresearch.com/autoit-sample-code
</url>
<language>english</language>
<text>
AutoIT Code Sample - Adding a shortcut in the user's Windows Startup folder in order to auto-start an AutoIT application every time a user logs-on to Windows. This script assumes that there is a checkbox defined on your Form, and will dynamically create or delete the shortcut on the check or uncheck event. ... While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 ;Do something Case $Checkbox1 If BitAND(GUICtrlRead($Checkbox1), $BN_CLICKED) = $BN_CLICKED Then If _GUICtrlButton_GetCheck($Checkbox1) Then ConsoleWrite("Checkbox checked... " & @CRLF) If Not FileExists(@StartupDir & "\linkName.lnk") Then FileCreateShortcut(@ScriptFullPath, @StartupDir & "\linkName.lnk", @ScriptDir, "", "AutoIT Script Description") EndIf Else ConsoleWrite("Checkbox unchecked... " & @CRLF) If FileExists(@StartupDir & "\linkName.lnk") Then You may also want to check for the presence of the shortcut when starting the application in order to properly display the state of the checkbox to the user. ... If FileExists(@StartupDir & "\linkName.lnk") Then _GUICtrlButton_SetCheck($Checkbox1) EndIf
</text>
<keywords>
<keyword>
<text>AutoIT Code Sample</text>
<relevance>0.990765</relevance>
</keyword>
<keyword>
<text>AutoIT Script Description</text>
<relevance>0.885643</relevance>
</keyword>
<keyword>
...
</keyword>
<keyword>
<text>nMsg</text>
<relevance>0.432048</relevance>
</keyword>
</keywords>
</results>
Here is the AutoIT code sample used to parse this XML file, using Msxml2.DOMDocument.3.0:
;===============================================================================
; Description:		XML Parsing in AutoIT using Msxml2.DOMDocument
; Author(s):		Videre Research, LLC - http://videreresearch.com
;===============================================================================

$feed = "http://url.to.xml.file/"
$oOXml = _XmlFileLoad ($feed)
$oXMLRoot = $oOXml.documentElement
$content = ""

$oXmlroot = $oOXml.documentElement

$objElement = $oXmlroot.getElementsByTagName("language")
If IsObj($objElement) Then ConsoleWrite($objElement(0).childNodes(0).nodeValue &@crlf)
;For $oXmlNode In $objElement
	;ConsoleWrite($oXmlNode.nodename & " - "  & $oXmlNode.text &@crlf)
;Next

$objElement = $oXmlroot.getElementsByTagName("text")
If IsObj($objElement) Then ConsoleWrite($objElement(0).childNodes(0).nodeValue &@crlf)
;For $oXmlNode In $objElement
	;ConsoleWrite($oXmlNode.nodename & " - "  & $oXmlNode.text &@crlf)
;Next

$objElement = $oXmlroot.getElementsByTagName("keyword")
For $oXmlNode In $objElement
	;ConsoleWrite($oXmlNode.nodename & " - "  & $oXmlNode.text &@crlf)
	For $oXmlNodeD In $oXmlNode.childNodes
		;ConsoleWrite($oXmlNodeD.nodename & " - "  & $oXmlNodeD.text &@crlf)
		Select
		Case $oXmlnodeD.nodename = "text"
			ConsoleWrite($oXmlnodeD.text &":"&@crlf)
		Case $oXmlnodeD.nodename = "relevance"
			ConsoleWrite($oXmlnodeD.text &@crlf)
		EndSelect
	Next

Next



;===============================================================================
; Function Name:	 _XmlFileLoad
; Description:		Load and parse the XML file
; Parameters:		$strFile	XML file to load
; Syntax:			_XmlBuildTree($objXml,$treeview)
; Author(s):		Stephen Podhajecki 
; Returns:			Object handle on success
;					0 on failure and sets error to parser error.
;===============================================================================
Func _XmlFileLoad($strXmlFile)
	$strFile = $strXmlFile
	;if not isobj($oXml) then
	$oXml = ObjCreate("Msxml2.DOMDocument.3.0")
	$oXml.async=0
	$oXml.load ($strFile)
	If $oXml.parseError.errorCode <> 0 Then
		MsgBox(4096, "Error", "Error opening specified file: " & $strFile & @CRLF & $oXml.parseError.reason)
		SetError($oXml.parseError.errorCode)
		Return 0
	EndIf
	Return $oXml
EndFunc   ;==>_XmlFileLoad
Comments