Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

XML DOM wrapper (COM)


  • Please log in to reply
742 replies to this topic

#41 eltorro

eltorro

    more or less the same as the latter of the former.

  • Active Members
  • PipPipPipPipPipPip
  • 596 posts

Posted 24 February 2006 - 07:29 AM

Steve,

Isn't that one of the main reasons for using CDATA so that you don't need to escape everything (since the CDATA is not parsed)?

Can you comment on the trouble using _XMLCreateCDATA? I reproduced the same outcome that Gary got trying to use it.

thanks,

Dale

Here's a fix for the write part:
Plain Text         
;=============================================================================== ; Function Name:    _XMLCreateCDATA ; Description:      Create a CDATA SECTION node directly under root. ; Parameters:       $node name of node to create ;                   $data CDATA value ; Syntax:           _XMLCreateCDATA($node,$data) ; Author(s):        Stephen Podhajecki <gehossafats@netmdc.com> ; Returns:          on error set error to 1 and returns -1 ; fixme, won't append to exisiting node. must create new node. ;=============================================================================== Func _XMLCreateCDATA($strNode, $strCDATA,$strNameSpc="")     Local $objChild, $objNode     While @error = 0         $objNode = $objDoc.createNode (1, $strNode, $strNameSpc)         $objChild = $objDoc.createCDATASection ($strCDATA)         $objNode.appendChild($objChild)         $objDoc.documentElement.appendChild ($objNode)         $objDoc.Save ($strFile)         $objChild = ""         Return     WEnd    ;    _XMLError( "Failed to create CDATA Section: " & $strNode & @CRLF & $oMyError.windescription)     _XMLError( "Failed to create CDATA Section: " & $strNode & @CRLF)     SetError(1)     Return -1 EndFunc  ;==>_XMLCreateCDATA


Here's the xml as loaded in SciTe
<?xml version="1.0"?> <Snippet><hCode><![CDATA[; Include Version:1.52 (8 February 2006) #include-once ; ------------------------------------------------------------------------------ ; ; AutoIt Version: 3.0 ; Language:    English ; Description:  Functions that assist with color management. ; ; ------------------------------------------------------------------------------ ;=============================================================================== ; ; Description:    Get the red component of a given color. ; Syntax:         ]]></hCode></Snippet>


I'm still working on the read. I think that the CDATASection is considered a child of the the node and may have to be referenced by it's index.

Steve

Edit: updated code.

Edited by eltorro, 24 February 2006 - 09:13 AM.








#42 eltorro

eltorro

    more or less the same as the latter of the former.

  • Active Members
  • PipPipPipPipPipPip
  • 596 posts

Posted 24 February 2006 - 09:18 AM

Here is the read part:
Plain Text         
;=============================================================================== ; Function Name:    _XMLGetValue ; Description:      Get XML Fieldbased on XPath input from root node. ; Parameters:       $path   xml tree path from root node (root/child/child..) ; Syntax:           _XMLGetValue($path) ; Author(s):        Stephen Podhajecki <gehossafats@netmdc.com> ; Returns:          array of fields text values -1 on failure ;=============================================================================== Func _XMLGetValue($strXPath)     Local $objNodeList, $arrResponse[1], $i     While @error = 0     $objNodeList = $objDoc.selectNodes($strXPath) ;   $objNodeList = $objDoc.selectNodes ($strXPath)         If $objNodeList.length > 0 Then             _DebugWrite("GetValue list length:" & $objNodeList.length)             For $objNode In $objNodeList             if $objNode.hasChildNodes() = False Then                 _ArrayAdd($arrResponse, $objNode.Text)             Else                 $objNodeChild = $objNode.childNodes(0)                 if $objNodeChild.nodeType = $NODE_CDATA_SECTION Then                     _ArrayAdd($arrResponse, $objNodeChild.data)                 Else                     _ArrayAdd($arrResponse, $objNode.Text)                 EndIf             EndIf                         _DebugWrite("GetValue:" & $objNode.nodeValue)         Next         Return $arrResponse ;           Return $objNode.nodeValue            ;            Return $arrResponse         Else             $xmlerr = @CRLF & "No matching node(s)found!"             Return -1             ExitLoop         EndIf             WEnd    ;    _XMLError( "Error Retrieving: " & $strXPath & @CRLF & $oMyError.windescription)     _XMLError( "Error Retrieving: " & $strXPath & $xmlerr)    ;            _XMLError( "No matching node(s)found for: " & $strXPath & @CRLF & $oMyError.windescription)     SetError(1)     Return -1     EndFunc  ;==>_XMLGetValue


The script for testing:
Plain Text         
#Include <_XMLDomWrapper.au3> #Include <String.au3> ;init some vars global $snips_dir=@ScriptDir $sXMLRoot = "Snippet" $s_TName ="test.test" ;open and read 512 bytes from color.au3 $s_TSource = FileOpen("C:\Program Files\AutoIt3\include\Color.au3",0) $s_TCode =FileRead($s_TSource,512) FileClose($s_TSource) ;get down to business. SaveSnippet($s_TName,$s_Tcode); save code snippet as cdata in xml $retVal = LoadSnippet($s_TName); read it back     if IsArray($retVal) Then         _ArrayDisplay($retVal,"hCode")     Else         MsgBox(0,"Code",LoadSnippet($s_TName))     EndIf Exit ;funcs. Func SaveSnippet($s_Name, $code)     Local $sFile = $snips_dir & '\' & $s_Name & '.xml'     While @error = 0         _XMLCreateFile ($sFile, $sXMLRoot, True)         _XMLFileOpen ($sFile) ;      _XMLCreateRootChild ("hCode")         _XMLCreateCDATA("hCode",$code); cdata needs new node ???         Return         ExitLoop     WEnd         MsgBox(4096, "Error", _XMLError ()) EndFunc;==>SaveSnippet Func LoadSnippet($s_Name)     Local $sFile = $snips_dir & '\' & $s_Name & '.xml', $ret, $s_code     ConsoleWrite($sFile & @LF)     If FileExists($sFile) Then         While @error = 0             $ret = _XMLFileOpen ($sFile)             $s_code = _XMLGetValue ("//hCode")              Return $s_code         WEnd         MsgBox(4096, "Error", _XMLError ())     EndIf EndFunc;==>LoadSnippet


#43 GaryFrost

GaryFrost

    I don't need your attitude. I have one of my own

  • Developers
  • 7,854 posts

Posted 24 February 2006 - 11:44 AM

Thanks for the help, I now have a new version of CSnippet out, saves the snippets to XML files

:o

Gary

SciTE for AutoItDirections for Submitting Standard UDFs

Don't argue with an idiot; people watching may not be able to tell the difference.


#44 GaryFrost

GaryFrost

    I don't need your attitude. I have one of my own

  • Developers
  • 7,854 posts

Posted 24 February 2006 - 12:30 PM

Forgot, had to clean up the include file i always use Opt('MustDeclareVars',1)

and remove variables not being used

Here's the cleaned up one, with the update functions also

Gary

Edit: Removed attachment

Edited by gafrost, 24 February 2006 - 03:56 PM.

SciTE for AutoItDirections for Submitting Standard UDFs

Don't argue with an idiot; people watching may not be able to tell the difference.


#45 eltorro

eltorro

    more or less the same as the latter of the former.

  • Active Members
  • PipPipPipPipPipPip
  • 596 posts

Posted 24 February 2006 - 03:31 PM

Thanks for the help, I now have a new version of CSnippet out, saves the snippets to XML files

:o

Gary


Great! :geek:

Forgot, had to clean up the include file i always use Opt('MustDeclareVars',1)

and remove variables not being used

Here's the cleaned up one, with the update functions also

Gary


Thanks, ;)

Steve

Udpated download in first post.

#46 eltorro

eltorro

    more or less the same as the latter of the former.

  • Active Members
  • PipPipPipPipPipPip
  • 596 posts

Posted 24 February 2006 - 06:51 PM

@KlemsonGuy

You can start with this code:
AutoIt         
;Stephen Podhajecki <gehossafats@netmdc.com>  #Include <_XMLDomWrapper.au3>  Local $sXmlFile, $oOXml, $my_ns    opt("MustDeclareVars", 1)  ;_SetDebug (True);show debug messages via console write  $sXmlFile = "C:\Program Files\AutoIt3\scripts\allocation.xml";******* put your path here.  If @error Then      MsgBox(4096, "File Open", "No file chosen")  Else      $oOXml = ""      $my_ns = ""      $oOXml = _XMLFileOpen ($sXmlFile, $my_ns)  EndIf  Dim $sallocation[200][4], $x  GetNode()  Exit  Func GetNode()      Local $x, $y, $ret, $iNodeCt, $msg      $iNodeCt = _XMLGetNodeCount ("//allocation/state", "", $NODE_ELEMENT)     ;    MsgBox(0, "Allocation", $iNodeCt)      For $x = 1 To $iNodeCt - 1          $ret = GetAlloc($x)          If IsArray($ret) Then              $msg = ""              For $y = 1 To UBound($ret) - 1                  $msg = $msg & "State = " & $ret[$y][4] & " ID = " & $ret[$y][0] & _                          " Comm = " & $ret[$y][1] & " Alloc = " & $ret[$y][2] & @LF              Next              ConsoleWrite("The Return Values:" & @LF & $msg)             ;MsgBox(0, "Allocations", $msg)          EndIf                Next  EndFunc  ;==>GetNode    Func GetAlloc($iIndex)      Local $iChildct, $iNodeCt, $sState, $sallocation[1][5], $aAttrN[1], $aAttrV[1], $ret      Local $x, $y, $z      $iIndex = "[" & $iIndex & "]"      $sState = _XMLGetAttrib ("//allocation/state" & $iIndex, "id")      $iChildct = _XMLGetNodeCount ("//allocation/state" & $iIndex & "/servicer", "", $NODE_ELEMENT)      _DebugWrite ("State>" & $sState & " Servicer ct>" & $iChildct & @LF)      If $iChildct > 0 Then ReDim $sallocation[$iChildct + 1][5]      For $y = 1 To $iChildct          If $y = 0 Then              $ret = _XMLGetAllAttrib ("//allocation/state" & $iIndex & "/servicer", $aAttrN, $aAttrV)          Else              $ret = _XMLGetAllAttrib ("//allocation/state" & $iIndex & "/servicer", $aAttrN, $aAttrV, "[" & $y & "]")          EndIf                    If @error = 0 Then              $sallocation[$y][0] = $aAttrV[0]              $sallocation[$y][4] = $sState              For $z = 1 To UBound($aAttrV) - 1                  $sallocation[$y][$z] = $aAttrV[$z]              Next          EndIf      Next      Return $sallocation  EndFunc  ;==>GetAlloc


Hope this is what you are looking for.

Steve

Edit: autoit code tags

Edited by eltorro, 26 January 2007 - 04:08 PM.


#47 rokey

rokey

    Seeker

  • Active Members
  • 6 posts

Posted 28 February 2006 - 03:06 PM

dear autoit-cracks

i have read all post about xml and played around with it... but i can't figure out how i must implement what i'm looking for. hopefully some could help me :o

i have the following xml file (could be found here)
Plain Text         
<buddyList>  <name>Wojjie's Buddy List</name> <buddy>     <name>Wojjie</name>     <status>Online</status>     <game>         <shortname>bf2</shortname>         <longname>BattleField 2</longname>     </game>     <server>         <name>24/7 Wake - Krackhouse</name>         <fullip>69.28.221.211:16567</fullip>         <link>http://www.game-monitor.com/GameServer/69.28.221.211:16567/</link>         <players>37/40</players>         <map>Wake Island 2007</map>     </server>     <score>12</score>     <ping>20 ms</ping>     <duration></duration> </buddy> <buddy>     <name>=GSF=FreekBoy</name>     <status>Offline</status>     <game>         <shortname>all</shortname>         <longname>All</longname>     </game>     <server>         <name></name>         <fullip>0.0.0.0:</fullip>         <link>http://www.game-monitor.com/GameServer/0.0.0.0:/</link>         <players></players>         <map></map>     </server>     <score></score>     <ping></ping>     <duration></duration> </buddy> <buddy>     <name>EVO_Matt</name>     <status>Offline</status>     <game>         <shortname>all</shortname>         <longname>All</longname>     </game>     <server>         <name></name>         <fullip>0.0.0.0:</fullip>         <link>http://www.game-monitor.com/GameServer/0.0.0.0:/</link>         <players></players>         <map></map>     </server>     <score></score>     <ping></ping>     <duration></duration> </buddy> <buddy>     <name>i365|klaude</name>     <status>Offline</status>     <game>         <shortname>all</shortname>         <longname>All</longname>     </game>     <server>         <name></name>         <fullip>0.0.0.0:</fullip>         <link>http://www.game-monitor.com/GameServer/0.0.0.0:/</link>         <players></players>         <map></map>     </server>     <score></score>     <ping></ping>     <duration></duration> </buddy> <buddy>     <name>i365|lreese</name>     <status>Offline</status>     <game>         <shortname>all</shortname>         <longname>All</longname>     </game>     <server>         <name></name>         <fullip>0.0.0.0:</fullip>         <link>http://www.game-monitor.com/GameServer/0.0.0.0:/</link>         <players></players>         <map></map>     </server>     <score></score>     <ping></ping>     <duration></duration> </buddy> <buddy>     <name>Saboom</name>     <status>Offline</status>     <game>         <shortname>all</shortname>         <longname>All</longname>     </game>     <server>         <name></name>         <fullip>0.0.0.0:</fullip>         <link>http://www.game-monitor.com/GameServer/0.0.0.0:/</link>         <players></players>         <map></map>     </server>     <score></score>     <ping></ping>     <duration></duration> </buddy> <buddy>     <name>[BWC]Rabbit</name>     <status>Offline</status>     <game>         <shortname>bf2</shortname>         <longname>BattleField 2</longname>     </game>     <server>         <name></name>         <fullip>0.0.0.0:</fullip>         <link>http://www.game-monitor.com/GameServer/0.0.0.0:/</link>         <players></players>         <map></map>     </server>     <score></score>     <ping></ping>     <duration></duration> </buddy> <buddy>     <name>^-DRz-^Picchioni</name>     <status>Offline</status>     <game>         <shortname>all</shortname>         <longname>All</longname>     </game>     <server>         <name></name>         <fullip>0.0.0.0:</fullip>         <link>http://www.game-monitor.com/GameServer/0.0.0.0:/</link>         <players></players>         <map></map>     </server>     <score></score>     <ping></ping>     <duration></duration> </buddy> </buddyList>


and i would like bring the information "Buddy Name", "Server Fullip", "Players" and the "Status" (changes the icon) in a gui, that it looks like this

Posted Image


it would be nice if someone could help my out! :geek:

#48 eltorro

eltorro

    more or less the same as the latter of the former.

  • Active Members
  • PipPipPipPipPipPip
  • 596 posts

Posted 02 March 2006 - 09:41 AM

dear autoit-cracks

i have read all post about xml and played around with it... but i can't figure out how i must implement what i'm looking for. hopefully some could help me :o


Posted Image


it would be nice if someone could help my out! :geek:

AutoIt         
#include <GUIConstants.au3>  #Include <_XMLDomWrapper.au3>  ;===============================================================================  ;Buddy list example  by <Stephen Podhajecki gehossafats@netmdc.com>  ;===============================================================================    Global $ledRed = "C:\Program Files\AutoIt3\Scripts\ledred.ico";change this to the script dir  Global $ledGrn = "C:\Program Files\AutoIt3\Scripts\ledgrn.ico";change this to the script dir  $Form1 = GUICreate("Buddy List", 315, 333, 192, 125)  GUISetBkColor(0xF0F0F0)    GUISetState(@SW_SHOW)  $msg = ""  opt("MustDeclareVars", 1)      Local $buddysheet = @ScriptDir & "\buddylist.xml"  ConsoleWrite($buddysheet)  Local $oBs = _XMLFileOpen ($buddysheet)  If @error Then      MsgBox(0, "Error", "There was an error opening the file.")      Exit  EndIf    FillGui()    While 1      $msg = GUIGetMsg()      Select          Case $msg = $GUI_EVENT_CLOSE              ExitLoop          Case Else            ;;;;;;;      EndSelect  WEnd  Exit  ;===============================================================================  ;Funcs  ;===============================================================================  Func _RetFirst($aArray); return first item in an array      If IsArray($aArray) Then          Return $aArray[1]      EndIf  EndFunc ;==>_RetFirst    Func _StringPad($sText, $sChar, $iLen, $iFlag = 0); add text to string      While StringLen($sText) < $iLen          If $iFlag < 1 Then              $sText = $sText & $sChar          Else              $sText = $sChar & $sText          EndIf      WEnd      Return $sText  EndFunc ;==>_StringPad    Func _ResizeGui($iht); resize gui to fit all buds.      Local $a = WinGetPos("")      WinMove("", "", $a[0], $a[1], $a[2], $iht + 40)  EndFunc ;==>_ResizeGui    Func FillGui(); fill the gui with the list.      Local $i_Budds = _XMLGetNodeCount ("//buddyList/*")      If $i_Budds > 0 Then          Local $x, $y[4], $z, $sRet = _XMLSelectNodes ("//buddyList/*")          If IsArray($sRet) Then            ;_ArrayDisplay($sRet,"Node Names")              _ResizeGui(20 * $sRet[0])              GUISetState(@SW_LOCK)              For $x = 1 To $sRet[0]                  If $sRet[$x] = "buddy" Then                      $y[0] = _RetFirst(_XMLGetField ("buddyList/*[" & $x & "]/status"))                      $y[1] = _StringPad(_RetFirst(_XMLGetField ("buddyList/*[" & $x & "]/name")), " ", 18)                      $y[2] = _StringPad(_RetFirst(_XMLGetField ("buddyList/*[" & $x & "]/server/fullip")), " ", 21)                      $y[3] = _StringPad(_RetFirst(_XMLGetField ("buddyList/*[" & $x & "]/server/players")), " ", 10)                      $z = 20* ($x - 1)                                            GUICtrlCreateLabel($y[2], 166, $z, 125, -1)                      GUICtrlCreateLabel($y[3], 275, $z, 85, -1)                      GUICtrlCreateLabel("         " & $y[1], 16, $z, 150, -1)                      If StringUpper($y[0]) = "ONLINE" Then                          GUICtrlCreateIcon($ledGrn, -1, 16, $z, 16, 16)                          GUICtrlSetStyle(-1, BitOR($SS_ICON, $SS_NOTIFY))                      Else                          GUICtrlCreateIcon($ledRed, -1, 16, $z, 16, 16)                          GUICtrlSetStyle(-1, BitOR($SS_ICON, $SS_NOTIFY))                      EndIf                    ;_ArrayDisplay($y,"Data")                  EndIf              Next              GUISetState(@SW_UNLOCK)          EndIf      EndIf  EndFunc ;==>FillGui


contains script and icons.
Attached File  buddylist.zip   2.95KB   787 downloads

Steve
Edit: changed to AutoIt code tags

Edited by eltorro, 26 January 2007 - 04:10 PM.


#49 Steve B.

Steve B.

    Seeker

  • New Members
  • 1 posts

Posted 02 March 2006 - 10:52 AM

Hi Guys!

Im wondering if there is a way to handle more than one XML File.

For Example combing Parts of XML to a new one?

Steve .B

#50 eltorro

eltorro

    more or less the same as the latter of the former.

  • Active Members
  • PipPipPipPipPipPip
  • 596 posts

Posted 02 March 2006 - 04:42 PM

Hi,

I just added another version of the wrapper. This one allows for working with multiple documents by supplying an index number to the object. The index is passed as the last optional parameter of every function so as to not interfere with previous operation.

Currently the file index is set for 5 files max[0-4]. Unless you have gobs of memory, I think that this is a resonable max as all docs are loaded into memory by M$'s dll. The global const $MAXFILES can be changed to meet your requirements.

This is basically a workaround and is considered an experimental version for testing.

Another option is to re-code the functions requiring that the doc object be pass in for every function.

At present, I don't have time to re-code.

Basically it works as follows for multi files:
Dim $myFiles[$MAXFILES]         $myFiles[0]= _XMLFileOpen("MyFile.xml","",0)           $myFiles[1] = _XMLFileOpen("Myfile1.xml","",1)         etc.......


Please see the first post for download link.


Steve.

Edited by eltorro, 02 March 2006 - 04:45 PM.


#51 rokey

rokey

    Seeker

  • Active Members
  • 6 posts

Posted 02 March 2006 - 05:07 PM

@eltorro

MANY THANKS!!
it works great! this is exactly what i'm looked for! ...again thanks.

#52 eltorro

eltorro

    more or less the same as the latter of the former.

  • Active Members
  • PipPipPipPipPipPip
  • 596 posts

Posted 11 March 2006 - 12:09 AM

I had someone ask me for a script to get the required items from an XSD and print them to a csv file.

Below is a basic script to do that. Please keep in mind that there may be bugs and it might not work on all xsd files.
AutoIt         
;AutoitXSDtool.au3, find required items in the XSD  ; ----------------------------------------------------------------------------  ;  ; AutoIt Version: 3.1.1.110  ; Author:         Stephen Podhajecki eltorro  ; Version: 0.1  ;  ; Script Function: find "required" items in the XSD, export data to csv  ;  ; ----------------------------------------------------------------------------    #Include "C:\Program Files\AutoIt3\include\_XMLDomMdi.au3"  #Include <Array.au3>  opt("MustDeclareVars", 1)  ;===============================================================================  Global $xsdFile  Global $count = 0  Global $sNxPath, $fHwnd  Global $sOutFile = @ScriptDir & "\" & @ScriptName & ".csv"  ;===============================================================================  ;$xsdFile=@ScriptDir & "\Base_v2.xsd"  $xsdFile = FileOpenDialog("Open XSD", @ScriptDir, "XSD (*.XSD)|XML (*.XML)", 1)  If @error Then      MsgBox(4096, "File Open", "No file chosen , Exiting")      Exit  EndIf    Main()    Exit    ;===============================================================================  ;Funcs  ;===============================================================================    Func Main()      Local $sPath, $aNodeName, $find, $ns, $oXSD      $ns = GetNameSpace()      $oXSD = _XMLFileOpen ($xsdFile, $ns)      If @error Or $oXSD < 1 Then          MsgBox(0, "Error", "There was an error opening the file " & $xsdFile)          $oXSD =0          Exit      EndIf      GetNameSpace()      $sPath = '.'      $aNodeName = _XMLGetChildNodes ($sPath)      If $aNodeName[0] > 0 Then          For $find = 1 To $aNodeName[0]              DrillDown($sPath & "/*" & '[' & $find & ']')          Next          If MsgBox(1, "Results", "There were " & $count & " required items." & @LF & _                  "Output is in " & $sOutFile & @LF & @LF & _                  "Click Ok to copy path to clipboard.") = 1 Then ClipPut($sOutFile)                Else          MsgBox(0, "Error:", "No nodes found for " & $sPath)      EndIf      $oXSD =0  EndFunc  ;==>Main    Func DrillDown($path)      If $sNxPath = "" Then $sNxPath = $path      Local $aNames[1], $aValues[1], $line = "", $retval, $sNodeName, $sData, $values, $sFndPath, $nc      $retval = String(_XMLGetAttrib ($path, "use"))      If $retval = "required" Then          $count = $count + 1          $sData = '"' & $path & '",'          $values = _XMLGetAllAttrib ($path, $aNames, $aValues)          For $y = 1 To UBound($aNames)              $sData = $sData & '"' & $aValues[$y - 1] & '",'          Next          $sData = StringTrimRight($sData, 1)          CSVOut($sData)      EndIf      $nc = _XMLGetNodeCount ($path & "/*")      $sNodeName = _XMLSelectNodes ($path & "/*")            If $nc > 0 Then          For $iNxNode = 1 To $sNodeName[0]              $sNxPath = $sNxPath & '/' & $sNodeName[$iNxNode] & '[' & $iNxNode & ']'              $sFndPath = $path & '/*[' & $iNxNode & ']'              DrillDown($sFndPath)          Next      Else          $sNxPath = ""      EndIf  EndFunc  ;==>DrillDown    Func CSVOut($sData)      If FileExists($sOutFile) Then          If $fHwnd = 0 Then              FileDelete($sOutFile)              $fHwnd = FileOpen($sOutFile, 9)          EndIf      Else          $fHwnd = FileOpen($sOutFile, 9)      EndIf      If $fHwnd > 0 Then          FileWriteLine($fHwnd, $sData)      EndIf  EndFunc  ;==>CSVOut    Func GetNameSpace()      Local $ns, $aAT[1], $aVl[1], $x, $ret,$oXSD      $oXSD= _XMLFileOpen ($xsdFile)      $ns = _XMLGetAllAttrib ("./*", $aAT, $aVl)      If $ns <> 0 Then          For $x = 0 To UBound($aAT) - 1              If StringLeft($aAT[$x], 5) = "xmlns" Then $ret = $aAT[$x] & "='" & $aVl[$x] & "'"          Next      EndIf      $oXSD =0      Return $ret  EndFunc  ;==>GetNameSpace


I hope that someone will find it useful. :)

Steve

Edited by eltorro, 26 January 2007 - 04:11 PM.


#53 big_daddy

big_daddy

  • Moderators
  • 2,591 posts

Posted 15 March 2006 - 05:12 PM

Wow, this XML stuff is giving me a headache.

First off here is the xml file I'm working with:


I want to turn it into something like this:


Basicly I want it to pull these values from //MACHINE/APPLICABLE
But if /MACHINE/APPLICABLE/EXPIRED == "True" then drop those.
NAME
KB_NUMBER
TYPE
SEVERITY
URL

I'm currently working on something, but if anyone could help out I'd be very greatful.
Okay, this is what I have so far:
#include <_XMLDomWrapper.au3> $sXmlFile = "MAIND53B.xml" $oOXml = _XMLFileOpen ($sXmlFile) $cnt = _XMLGetNodeCount ('//MACHINE/APPLICABLE/*', "") Dim $aAttrName[1], $aAttrValue[1] For $i = 0 To $cnt - 1     $ret = _XMLGetAllAttribIndex ("//MACHINE/APPLICABLE/*", $aAttrName, $aAttrValue, "", $i)     If IsArray($aAttrName) Then         _ArrayDisplay($aAttrName, "Attrib Names")         _ArrayDisplay($aAttrValue, "Attrib Values")     Else         MsgBox(0, "Error", "No items found.")         Exit     EndIf Next

Edited by big_daddy, 15 March 2006 - 05:42 PM.


#54 eltorro

eltorro

    more or less the same as the latter of the former.

  • Active Members
  • PipPipPipPipPipPip
  • 596 posts

Posted 15 March 2006 - 07:15 PM

Here you go:
AutoIt         
;MAIN53B.au3, find un-expired updates  ; ----------------------------------------------------------------------------  ;  ; AutoIt Version: 3.1.1.110  ; Author:         Stephen Podhajecki eltorro  ; Version: 0.1  ;  ; Script Function: find un-expired updates, export data to csv  ;  ; ----------------------------------------------------------------------------    #Include "C:\Program Files\AutoIt3\include\_XMLDomMdi.au3"  #Include <Array.au3>  ;_SetDebug(True)  opt("MustDeclareVars", 1)  ;===============================================================================  Global $xmlFile  Global $count = 0  Global $sNxPath, $fHwnd  Global $sOutFile = @ScriptDir & "\" & @ScriptName & ".csv";Change this to suit your needs  ;===============================================================================  ;$xsdFile=@ScriptDir & "\Base_v2.xsd"  $xmlFile = FileOpenDialog("Open XML", @ScriptDir, "XML (*.XML)", 1)  If @error Then      MsgBox(4096, "File Open", "No file chosen , Exiting")      Exit  EndIf    Main()    Exit  ;===============================================================================  ;Funcs  ;===============================================================================  Func Main()      Local $szXPath, $aNodeName, $find, $ns, $oXSD      $ns = GetNameSpace()      $oXSD = _XMLFileOpen ($xmlFile, $ns)      If @error Or $oXSD < 1 Then          MsgBox(0, "Error", "There was an error opening the file " & $xmlFile)          $oXSD = 0          Exit      EndIf      $szXPath = "//INVENTORY/*"      $aNodeName = _XMLGetChildNodes ($szXPath)      If $aNodeName <> - 1 Then          For $find = 0 To $aNodeName[0]              ConsoleWrite($aNodeName[$find])              DrillDown($szXPath & "/*" & '[' & $find & ']')          Next          If MsgBox(1, "Results", "There were " & $count & " items found." & @LF & _                  "Output is in " & $sOutFile & @LF & @LF & _                  "Click Ok to copy path to clipboard.") = 1 Then ClipPut($sOutFile)                Else          MsgBox(0, "Error:", "No nodes found for " & $szXPath)      EndIf      $oXSD = 0  EndFunc;==>Main    Func GetNameSpace()      Local $ns, $aAT[1], $aVl[1], $x, $ret, $oXSD      $oXSD = _XMLFileOpen ($xmlFile)      $ns = _XMLGetAllAttrib ("./*", $aAT, $aVl)      If $ns <> 0 Then          For $x = 0 To UBound($aAT) - 1              If StringLeft($aAT[$x], 5) = "xmlns" Then $ret = $aAT[$x] & "='" & $aVl[$x] & "'"          Next      EndIf      $oXSD = 0      Return $ret  EndFunc;==>GetNameSpace    Func CSVCreate($szFileName, $sHeader)      If FileExists($szFileName) Then          If $fHwnd = 0 Then; if we don't have a handle then old file.              FileDelete($sOutFile)              $fHwnd = FileOpen($sOutFile, 9)          EndIf      Else          $fHwnd = FileOpen($sOutFile, 9)                EndIf      If $fHwnd Then CSVOut($sHeader)      Return $fHwnd  EndFunc;==>CSVCreate    Func CSVOut($sData)      If $fHwnd > 0 Then          FileWriteLine($fHwnd, $sData)      EndIf  EndFunc;==>CSVOut    Func DrillDown($path)      If $sNxPath = "" Then $sNxPath = $path      Local $aNames[1], $aValues[1], $line = "", $retval, $sNodeName, $sData, $values, $sFndPath, $nc      Local $y, $x, $sRet, $aSearch = _ArrayCreate ("NAME", "KB_NUMBER", "TYPE", "SEVERITY", "URL")      $retval = String(_XMLGetAttrib ($path, "EXPIRED"))      If $retval = "False" Then          If Not $fHwnd Then CSVCreate($sOutFile, "Update Name,KB Number,Type,Severity,URL")          $count = $count + 1          $sData = ""          $values = _XMLGetAllAttrib ($path, $aNames, $aValues)      ;make csv for Update Name,KB Number,Type,Severity,URL      ;if not @error then     _ArrayDisplay($aValues,"")          For $x = 0 To 4              $sRet = ""          ;find attribute names we want and get the corresponding values              For $y = 0 To UBound($aNames) - 1                  If $aNames[$y] = $aSearch[$x] Then                      $sRet = $aValues[$y]                      ExitLoop                  EndIf              Next              $sData = $sData & $sRet & ','          Next          $sData = StringTrimRight($sData, 1)      ;CSVOut("["&$count&"]"&$sData)          CSVOut($sData)      EndIf      $nc = _XMLGetNodeCount ($path & "/*")      $sNodeName = _XMLSelectNodes ($path & "/*")            If $nc > 0 Then          For $iNxNode = 1 To $sNodeName[0]              $sNxPath = $sNxPath & '/' & $sNodeName[$iNxNode] & '[' & $iNxNode & ']'              $sFndPath = $path & '/*[' & $iNxNode & ']'              DrillDown($sFndPath)          Next      Else          $sNxPath = ""      EndIf  EndFunc;==>DrillDown


Change the $sOutFile to suit your needs

Edit:
The script runs the whole xml file.
to run applicable use
$szXPath = "//INVENTORY/MACHINE/APPLICABLE"
Edit: Added Autoit code tags


Steve

Edited by eltorro, 26 January 2007 - 04:12 PM.


#55 big_daddy

big_daddy

  • Moderators
  • 2,591 posts

Posted 15 March 2006 - 09:23 PM

Here you go:

It worked perfectly! Thank you for the help.

#56 eltorro

eltorro

    more or less the same as the latter of the former.

  • Active Members
  • PipPipPipPipPipPip
  • 596 posts

Posted 15 March 2006 - 09:31 PM

It worked perfectly! Thank you for the help.


Not a problem. Glad I could help. :)

#57 nfwu

nfwu

    I'm not active on these forums

  • Active Members
  • PipPipPipPipPipPip
  • 1,234 posts

Posted 16 March 2006 - 03:17 AM

Really confused: Where is the download?
???
#)

EDIT: forget it, i figured it's _XMLMdiDOM.au3
Nice work! this goes into my "autoit scripts and scraps" folder!

Edited by nfwu, 16 March 2006 - 03:19 AM.


#58 ralnee

ralnee

    Seeker

  • Active Members
  • 13 posts

Posted 04 April 2006 - 02:27 AM

I'd like to try my hand at making an rss reader (as a noob), but I'm unable to walk the xml tree in a rdf file, using the examples shown here already. So, after two days of trying, I'd figured I'd ask. Below I have a sample rss feed, that I like to parse into a two pane window (feed leafs on left with headlines, and article on right).

Here's a sample feed.
Thanks in Advance

=====START=====
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>mysite.com</title>
<link>http://www.mysite.com/rssclick/?section=mysite_topstories</link>
<description>mysite.com delivers up-to-the-minute news and information on the latest top stories, weather, entertainment, politics and more.</description>
<language>en-us</language>
<copyright>© 2006 musite.com.</copyright>
<pubDate>Mon, 03 Apr 2006 19:54:53 EDT</pubDate>
<ttl>5</ttl>
<image>
<title>xxx.com</title>
<link>http://www.mysite.com/rssclick/?section=mysite_topstories</link>
<url>http://i.mysite.net/mysite/.element/img/1.0/logo/mysite.logo.rss.gif</url>
<width>144</width>
<height>33</height>
<description>mysite.com delivers up-to-the-minute news and information on the latest top stories, weather, entertainment, politics and more.</description>
</image>
<item>
<title>Moussaoui: God curse you all</title>
<link>http://www.mysite.com/rssclick/2006/LAW/04/03/moussaoui.verdict/index.html?section=mysite_topstories</link>
<description>Al Qaeda conspirator Zacarias Moussaoui is eligible for the death penalty, a federal jury decided Monday in the first U.S. trial about the September 11, 2001, attacks. Jurors agreed with prosecutors that his lies to FBI agents resulted in 9/11 deaths. After jurors left the courtroom, Moussaoui shouted, "You'll never get my blood. God curse you all."</description>
<pubDate>Mon, 03 Apr 2006 18:40:38 EDT</pubDate>
</item>
<item>
<title>Replacement organs grown from patients' own cells</title>
<link>http://www.mysite.com/rssclick/2006/HEALTH/conditions/04/03/engineered.organs/index.html?section=mysite_topstories</link>
<description>Kaitlyne McNamara no longer worries about feeling different at school.</description>
<pubDate>Mon, 03 Apr 2006 18:32:04 EDT</pubDate>
</item>
</channel>
</rss>


======END=========



#59 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 15,730 posts

Posted 04 April 2006 - 03:49 AM

Answered your question here... in the future, if you've tried something... post what you've tried, otherwise all you say you've done is just "words" :). http://www.autoitscript.com/forum/index.ph...ndpost&p=168994

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.


#60 ralnee

ralnee

    Seeker

  • Active Members
  • 13 posts

Posted 04 April 2006 - 04:46 AM

Thanks for the help. I'll try it.....




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users