Max Kipness Posted January 4, 2008 Posted January 4, 2008 Hello, does anyone know of a simple XML to Array function? I know there is an XML UDF out there and I've looked at it, but it appears to be very complex. I really just want to pull all data out into an associative array like the many XML2Array PHP scripts out there. Or is there an easy way to do this with the XML udf? Thanks, Max
weaponx Posted January 4, 2008 Posted January 4, 2008 This can be done with the XML UDF, I have used it quite a bit. Post a sample portion of your xml so we can take a look.
lsakizada Posted January 4, 2008 Posted January 4, 2008 (edited) Hello, does anyone know of a simple XML to Array function? I know there is an XML UDF out there and I've looked at it, but it appears to be very complex. I really just want to pull all data out into an associative array like the many XML2Array PHP scripts out there. Or is there an easy way to do this with the XML udf? Thanks, Max do something like that: Func _ReadXMFileToArray(ByRef $aGetAllGateways, $XMLFileName) If Not _FileReadToArray(@ScriptDir & "\" & $XMLFileName, $aGetAllGateways) Then MsgBox(4096, "Error", " Error reading XMl file to Array error") Exit EndIf Return $aGetAllGateways EndFunc ;==>_ReadXMFileToArray Edited January 4, 2008 by lsakizada Be Green Now or Never (BGNN)!
weaponx Posted January 4, 2008 Posted January 4, 2008 XML elements can span multiple lines, reading an xml file to array is useless. It has to be parsed and that is best done by the premade UDF.
lsakizada Posted January 4, 2008 Posted January 4, 2008 (edited) XML elements can span multiple lines, reading an xml file to array is useless. It has to be parsed and that is best done by the premade UDF. I am sure you have not tested the function to understand how simple is it before you posted your comment. Each line of the file goes into array item. Then should use this block to manipulate each line of the file. For $I =0 to UBound ($Array) -1 Next then using this function to get the data between the <></> nodes: Func stringbetween($str, $start, $end) $pos = StringInStr($str, $start) If Not @error Then $str = StringTrimLeft($str, $pos + StringLen($start) - 1) $pos = StringInStr($str, $end) If Not @error Then $str = StringTrimRight($str, StringLen($str) - $pos + 1) Return $str EndIf EndIf EndFunc the last function I grabed it somewhere from the forum. to be more specified use it this way: Local $count = 0, $st = "" For $i = 1 To UBound($aGetAllGateways) - 1 $st = stringbetween(StringStripWS($aGetAllGateways[$i], 8), '<id>', '</id>') If $st <> "" Then $count = $count + 1 _ArrayInsert($aGatewayIDs, $count, $st) EndIf Next Edited January 4, 2008 by lsakizada Be Green Now or Never (BGNN)!
weaponx Posted January 4, 2008 Posted January 4, 2008 You would have to write a very extensive parser to break valid xml into an array.
lsakizada Posted January 4, 2008 Posted January 4, 2008 You would have to write a very extensive parser to break valid xml into an array.Again, please test the function before you post your comment . it is working .. Be Green Now or Never (BGNN)!
weaponx Posted January 4, 2008 Posted January 4, 2008 (edited) I think you are barking up the wrong tree. Below is valid XML. Show me how you would easily get all of the Chevrolet models without a parser or static regular expression.Original XML:<?xml version="1.0"?> <cars> <make name="Toyota"> <model name="MR2"> <year start="1991" end="1995" filename="SWF\TOYOTA_MR2_1991-1995.swf"/> <year start="1999" end="2007"/> </model> <model name="Supra"></model> </make> <make name="Ford"> <model name="Mustang"> <year start="2006" end="2009" filename="SWF\FORD_MUSTANG_2006-2009.swf"/> </model> <model name="GT"></model> </make> <make name="Chevrolet"> <model name="Corvette"></model> <model name="Cobalt"></model> </make> </cars>Same thing...one line:<?xml version="1.0"?><cars><make name="Toyota"> <model name="MR2"> <year start="1991" end="1995" filename="SWF\TOYOTA_MR2_1991-1995.swf"/><year start="1999" end="2007"/></model><model name="Supra"></model></make><make name="Ford"><model name="Mustang"><year start="2006" end="2009" filename="SWF\FORD_MUSTANG_2006-2009.swf"/></model><model name="GT"></model></make><make name="Chevrolet"><model name="Corvette"></model><model name="Cobalt"></model></make></cars>Example using XML UDF (Output all Toyota models to console):#include <_XMLDomWrapper.au3> #include <Array.au3> $sXMLFile = "cars.xml" $result = _XMLFileOpen($sXMLFile) if $result = 0 then Exit $sXPath = '//make[@name = "Toyota"]/model' $nodeArray = _XMLSelectNodes($sXPath) ;Write all models to console For $X = 1 to $nodeArray[0] ConsoleWrite(_XMLGetAttrib ($sXPath & "[" & $X & "]", "name") & @CRLF) Next Edited January 4, 2008 by weaponx
lsakizada Posted January 4, 2008 Posted January 4, 2008 I think you are barking up the wrong tree. Below is valid XML. Show me how you would easily get all of the Chevrolet models without a parser or static regular expression. Original XML: <?xml version="1.0"?> <cars> <make name="Toyota"> <model name="MR2"> <year start="1991" end="1995" filename="SWF\TOYOTA_MR2_1991-1995.swf"/> <year start="1999" end="2007"/> </model> <model name="Supra"></model> </make> <make name="Ford"> <model name="Mustang"> <year start="2006" end="2009" filename="SWF\FORD_MUSTANG_2006-2009.swf"/> </model> <model name="GT"></model> </make> <make name="Chevrolet"> <model name="Corvette"></model> <model name="Cobalt"></model> </make> </cars> Same thing...one line: <?xml version="1.0"?><cars><make name="Toyota"> <model name="MR2"> <year start="1991" end="1995" filename="SWF\TOYOTA_MR2_1991-1995.swf"/><year start="1999" end="2007"/></model><model name="Supra"></model></make><make name="Ford"><model name="Mustang"><year start="2006" end="2009" filename="SWF\FORD_MUSTANG_2006-2009.swf"/></model><model name="GT"></model></make><make name="Chevrolet"><model name="Corvette"></model><model name="Cobalt"></model></make></cars> We need regular express for sure, but let Max Kipness post his xml first to handle his request. Be Green Now or Never (BGNN)!
ptrex Posted January 4, 2008 Posted January 4, 2008 @lsakizadaweaponx is right. For parsing XML you need to use the DOM object model !!XML schoolAll the rest is wasting time.regards,ptrex Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New
Max Kipness Posted January 4, 2008 Author Posted January 4, 2008 We need regular express for sure, but let Max Kipness post his xml first to handle his request. The XML code posted by Weaponx is a good starting point. I'm just looking for something that works on any generic xml code. As I stated I use something similar in PHP. Just throw any PHP code to it, and it creates an array. Here are the results from running Weaponx's code through my PHP parser. Sorry, but unfortunately when you post here, it flattens out all the tab spacing, so everything is left justified and hard to read. I've now played around with the XML UDF a bit and was able to extract some of the data, but it just seems like this type of approach is easier?? Array ( [make] => Array ( [0] => Array ( [@attributes] => Array ( [name] => Toyota ) [model] => Array ( [0] => Array ( [@attributes] => Array ( [name] => MR2 ) [year] => Array ( [0] => Array ( [@attributes] => Array ( [start] => 1991 [end] => 1995 [filename] => SWF\TOYOTA_MR2_1991-1995.swf ) ) [1] => Array ( [@attributes] => Array ( [start] => 1999 [end] => 2007 ) ) ) ) [1] => Array ( [@attributes] => Array ( [name] => Supra ) ) ) ) [1] => Array ( [@attributes] => Array ( [name] => Ford ) [model] => Array ( [0] => Array ( [@attributes] => Array ( [name] => Mustang ) [year] => Array ( [@attributes] => Array ( [start] => 2006 [end] => 2009 [filename] => SWF\FORD_MUSTANG_2006-2009.swf ) ) ) [1] => Array ( [@attributes] => Array ( [name] => GT ) ) ) ) [2] => Array ( [@attributes] => Array ( [name] => Chevrolet ) [model] => Array ( [0] => Array ( [@attributes] => Array ( [name] => Corvette ) ) [1] => Array ( [@attributes] => Array ( [name] => Cobalt ) ) ) ) ) )
SpookMeister Posted January 4, 2008 Posted January 4, 2008 You can clean it up by pasting it inside [code ] [/code ] labels. Or better yet, [autoit ] [/autoit ] Note: remove the extra space I added to the tags [u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]
GEOSoft Posted January 4, 2008 Posted January 4, 2008 You can clean it up by pasting it inside [code ] [/code ] labels. Or better yet, [autoit ] [/autoit ] Note: remove the extra space I added to the tagsCareful with those AutoIt tags. You can end up with an unreadable code block. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
Max Kipness Posted January 4, 2008 Author Posted January 4, 2008 You can clean it up by pasting it inside [code ] [/code ] labels. Or better yet, [autoit ] [/autoit ] Note: remove the extra space I added to the tags Cool, thanks. That makes it much easier to read. So, again this is the results from the PHP xml to array function. Works on any xml code you can throw at it. My opinion is that it's easiest to work with the data once it's in this format as it's easy to loop through the data. Max expandcollapse popupArray ( [make] => Array ( [0] => Array ( [@attributes] => Array ( [name] => Toyota ) [model] => Array ( [0] => Array ( [@attributes] => Array ( [name] => MR2 ) [year] => Array ( [0] => Array ( [@attributes] => Array ( [start] => 1991 [end] => 1995 [filename] => SWF\TOYOTA_MR2_1991-1995.swf ) ) [1] => Array ( [@attributes] => Array ( [start] => 1999 [end] => 2007 ) ) ) ) [1] => Array ( [@attributes] => Array ( [name] => Supra ) ) ) ) [1] => Array ( [@attributes] => Array ( [name] => Ford ) [model] => Array ( [0] => Array ( [@attributes] => Array ( [name] => Mustang ) [year] => Array ( [@attributes] => Array ( [start] => 2006 [end] => 2009 [filename] => SWF\FORD_MUSTANG_2006-2009.swf ) ) ) [1] => Array ( [@attributes] => Array ( [name] => GT ) ) ) ) [2] => Array ( [@attributes] => Array ( [name] => Chevrolet ) [model] => Array ( [0] => Array ( [@attributes] => Array ( [name] => Corvette ) ) [1] => Array ( [@attributes] => Array ( [name] => Cobalt ) ) ) ) ) )
weaponx Posted January 4, 2008 Posted January 4, 2008 I think you skimmed over the AutoIt code I posted under my xml example. It is very simple to get EXACTLY what you need from the xml.
Max Kipness Posted January 4, 2008 Author Posted January 4, 2008 I think you skimmed over the AutoIt code I posted under my xml example.It is very simple to get EXACTLY what you need from the xml.You know what, as I was watching you and the other guy go back an forth I never noticed your little snippet of code. I will try it and hopefully it will work for what I need.Thanks,Max
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now