GZE Posted October 8, 2014 Share Posted October 8, 2014 Hey, Is there an (easy) way to convert a xml or json file to a 2D array? I have been looking arround the forum but, by lack of srcipting talent, failed to find a solution The output array should look like this (it will be more collomns with the real data set): Id Name 550 Name01 537 Name02 519 Name03 ... The data to be converted comes from a rest api respons and can be xlm or json... <UserStories> <UserStory Id="550" Name="name01" /> <UserStory Id="537" Name="name02" /> <UserStory Id="519" Name="name03" /> <UserStory Id="516" Name="name04" /> <UserStory Id="514" Name="name05" /> <UserStory Id="512" Name="name06" /> <UserStory Id="509" Name="name07" /> <UserStory Id="469" Name="name08" /> <UserStory Id="468" Name="name09" /> <UserStory Id="442" Name="name10" /> </UserStories> expandcollapse popup{ "Items": [ { "Id": 537, "Name": "Name01" }, { "Id": 519, "Name": "Name02" }, { "Id": 516, "Name": "Name03" }, { "Id": 514, "Name": "Name03" }, { "Id": 512, "Name": "Name04" }, { "Id": 509, "Name": "Name05" }, { "Id": 469, "Name": "Name06" }, { "Id": 468, "Name": "Name07" }, { "Id": 442, "Name": "Name08" }, { "Id": 440, "Name": "Name09" }, { "Id": 433, "Name": "Name10" }, ] } Thanks in advance, Simon Link to comment Share on other sites More sharing options...
MikahS Posted October 8, 2014 Share Posted October 8, 2014 Have a look at the >JSON UDF library. Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4 Feel free to use any of my code for your own use. Forum FAQ Link to comment Share on other sites More sharing options...
mikell Posted October 8, 2014 Share Posted October 8, 2014 (edited) Assuming that an 'Id' contains digits only and 'Name' is never empty, you can try something like this (which works in both cases) #Include <Array.au3> $txt = FileRead("1.txt") ; your xml or json file $res = StringRegExp($txt, '\WId\W+(\d+)\W+Name\W+([^"]+)', 3) Local $array[UBound($res)/2][2] For $i = 0 to UBound($res)-1 If Mod($i, 2) = 0 Then $array[$i/2][0] = $res[$i] Else $array[($i-1)/2][1] = $res[$i] EndIf Next _ArrayDisplay($array) Edited October 8, 2014 by mikell Link to comment Share on other sites More sharing options...
jdelaney Posted October 9, 2014 Share Posted October 9, 2014 XMLDom: #include <Array.au3> $sXML = '<UserStories>' & _ '<UserStory Id="550" Name="name01" />' & _ '<UserStory Id="537" Name="name02" />' & _ '<UserStory Id="519" Name="name03" />' & _ '<UserStory Id="516" Name="name04" />' & _ '<UserStory Id="514" Name="name05" />' & _ '<UserStory Id="512" Name="name06" />' & _ '<UserStory Id="509" Name="name07" />' & _ '<UserStory Id="469" Name="name08" />' & _ '<UserStory Id="468" Name="name09" />' & _ '<UserStory Id="442" Name="name10" />' & _ '</UserStories>' $oXML = ObjCreate("Microsoft.XMLDOM") $oXML.loadxml($sXML) $oStories = $oXML.SelectNodes('//UserStory') Local $array For $oStory In $oStories $id = $oStory.GetAttribute("Id") $name = $oStory.GetAttribute("Name") If IsArray($array) Then ReDim $array[UBound($array)+1][2] $array[UBound($array)-1][0] = $id $array[UBound($array)-1][1] = $name Else Local $array[1][2] $array[UBound($array)-1][0] = $id $array[UBound($array)-1][1] = $name EndIf Next _ArrayDisplay($array) Probably better to do what you need within the loop, but you asked for an array, so... IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
mLipok Posted October 9, 2014 Share Posted October 9, 2014 GZE open this WIKI page: https://www.autoitscript.com/wiki/User_Defined_Functions and try to find XML and JSON mLipok Signature beginning:* Please remember: "AutoIt"..... * Wondering who uses AutoIt and what it can be used for ? * Forum Rules ** ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Code * for other useful stuff click the following button: Spoiler Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API * ErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 * My contribution to others projects or UDF based on others projects: * _sql.au3 UDF * POP3.au3 UDF * RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane * Useful links: * Forum Rules * Forum etiquette * Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * Wiki: * Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX IE Related: * How to use IE.au3 UDF with AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler * IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related: * How to get reference to PDF object embeded in IE * IE on Windows 11 * I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions * EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *I also encourage you to check awesome @trancexx code: * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuff * OnHungApp handler * Avoid "AutoIt Error" message box in unknown errors * HTML editor * winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/ "Homo sum; humani nil a me alienum puto" - Publius Terentius Afer"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming" , be and \\//_. Anticipating Errors : "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty." Signature last update: 2023-04-24 Link to comment Share on other sites More sharing options...
Shane0000 Posted October 9, 2014 Share Posted October 9, 2014 '?do=embed' frameborder='0' data-embedContent>> I have been using this one , i haven't checked out the UDF above Link to comment Share on other sites More sharing options...
GZE Posted October 11, 2014 Author Share Posted October 11, 2014 (edited) Hello, Thanks for the assistance... It amazes me how responsive this forum is!!! I used the code from jdelaney as primary inspiration and altered it to be independent from the number of columns. Looking at the code now it looks quit simple but it was, for me, quit a struggle to get the syntax right. As many of you already noticed my scripting skills basically end(ed) after arrays. This is the code i ended up with: expandcollapse popup#include <Array.au3> $sXML = '<UserStories>' & _ '<UserStory Id="550" Name="name01" />' & _ '<UserStory Id="537" Name="name02" />' & _ '<UserStory Id="519" Name="name03" />' & _ '<UserStory Id="516" Name="name04" />' & _ '<UserStory Id="514" Name="name05" />' & _ '<UserStory Id="512" Name="name06" />' & _ '<UserStory Id="509" Name="name07" />' & _ '<UserStory Id="469" Name="name08" />' & _ '<UserStory Id="468" Name="name09" />' & _ '<UserStory Id="442" Name="name10" />' & _ '</UserStories>' _ArrayDisplay(XmlToArray($sXML)) Func XmlToArray($sXML) local $resultArray[0][0] ; Create XML OBJECT $oXML = ObjCreate("Microsoft.XMLDOM") $oXML.loadxml($sXML) $oRecords = $oXML.documentElement.childNodes ; Create Array with headers in row 0 $length = $oRecords.item(0).attributes.length ReDim $resultArray[1][$length] $j = 0 For $name in $oRecords.item(0).attributes $name = $oRecords.item(0).attributes($j).name $resultArray[0][$j] = $name $j+=1 Next ; Fill array with values For $oRecord In $oRecords ReDim $resultArray[UBound($resultArray, $UBOUND_ROWS)+1][UBound($resultArray, $UBOUND_COLUMNS)] For $j = 0 To UBound($resultArray, $UBOUND_COLUMNS) - 1 Step +1 $attributeName = $resultArray[0][$j] $attributeValue = $oRecord.GetAttribute($attributeName) $resultArray[UBound($resultArray)-1][$j] = $attributeValue Next Next Return $resultArray EndFunc FYI... My little scripting project is to create some reporting (eg cumulative flow diagram, throughput,..) from data pulled (rest api) from an online Kanban board. Until now i had to export csv files manually from the site (the csv''s where off course read into arrays); this function will eliminate this manual step (not yet the arrays ). Can SciTe autocomplete XML DOM syntax? Thanks again for all the responses, Simon Edited October 11, 2014 by GZE Link to comment Share on other sites More sharing options...
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