Leaderboard
Popular Content
Showing content with the highest reputation on 10/02/2022 in all areas
-
Xml2Json UDF - Transform XML to JSON
AspirinJunkie and one other reacted to TheXman for a topic
The tools & UDF's used to parse and process JSON are much more flexible and powerful than the ones for XML. In general, they are also much faster. So I created this Xml2Json UDF to tranform XML to JSON. It consists of 2 functions, _Xml2Json() and _XmlFile2Json(). ;================================================================================================= ; _Xml2Json($sXml, $sXsl = Default) ; ; Author: TheXman (https://www.autoitscript.com/forum/profile/23259-thexman/?tab=field_core_pfield_11) ; ; Description: Transform XML string to JSON ; Parameter(s): $sXml - A string containing a valid XML document. ; $sXsl - [optional] A string containing the XSL stylesheet used for transform. ; Return Value(s): Success - A string containing JSON ; Failure - "" and sets @error to a non-zero value. ; @error 1 = Unable to create XML object ; 2 = Unable to create XSL object ; 3 = Unable to parse XML. Make sure XML is valid. ; 4 = Unable to parse XSL. Make sure XSL is valid. ; 5 = XML Transform failed. Make sure you are using a valid v1.0 stylesheet. ; ; Remarks: MSXML 6.0, the COM component used to do the tranformations, only processes ; XSLT 1.0 stylesheets. If you pass a version 2.0+ stylesheet, you WILL get an ; error. ; ; The XSL stylesheet used by default came from: ; https://www.bjelic.net/2012/08/01/coding/convert-xml-to-json-using-xslt/ ; MIT License - Copyright (c) 2012 Bojan Bjelic ;================================================================================================= ;================================================================================================= ; _XmlFile2Json($sXmlFile, $sXsl = Default) ; ; Author: TheXman (https://www.autoitscript.com/forum/profile/23259-thexman/?tab=field_core_pfield_11) ; ; Description: Transform XML file to JSON ; Parameter(s): $sXmlFile - A string containing a valid XML document file path. ; $sXsl - [optional] A string containing the XSL stylesheet used for transform. ; Return Value(s): Success - A string containing the transformed JSON. ; Failure - "" and sets @error to a non-zero value. ; @error -1 = File does not exist ; 1 = Unable to create XML object ; 2 = Unable to create XSL object ; 3 = Unable to parse XML. Make sure XML is valid. ; 4 = Unable to parse XSL. Make sure XSL is valid. ; 5 = XML Transform failed. Make sure you are using a valid v1.0 stylesheet. ; ; Remarks: MSXML 6.0, the COM component used to do the tranformations, only processes ; XSLT 1.0 stylesheets. If you pass a version 2.0+ stylesheet, you WILL get an ; error. ; ; The XSL stylesheet used by default came from: ; https://www.bjelic.net/2012/08/01/coding/convert-xml-to-json-using-xslt/ ; MIT License - Copyright (c) 2012 Bojan Bjelic ;================================================================================================= The transformation is done by doing an XML Transform using a xml-to-json XSL stylesheet. If you want to use your own xml-to-json XSL stylesheet instead of the default one, then you can supply it in the 2nd parameter. The only caveat is that all values are transformed to strings. That means that numeric XML values will be returned as a strings. For example, the numeric value 10.50 will be returned as "10.50". The UDF comes with a couple of example scripts. One example script, which is below, just shows a sample transformation using the included sample XML file. The second example does the transformation and queries the transformed JSON for information. Example-01 ( uses the included sample XML file, Books.xml ) #AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d #include <Constants.au3> #include "xml2json.au3" #include "jq.au3" ; (https://www.autoitscript.com/forum/files/file/502-jq-udf-a-powerful-flexible-json-processor/) example() Func example() Const $XML_FILE = "books.xml" Local $hTimer = -1 Local $sCmdOutput = "", _ $sJson = "" ;Initialize jq environment _jqInit("C:\Utils\JQ\jq-win64.exe") ;<== Path to jq exe If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Unable to find jq executable - @error = " & @error) $hTimer = TimerInit() ;Convert XML to JSON $sJson = _XmlFile2Json($XML_FILE) If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "_xmlfile2json() Error", "@error = " & @error) ConsoleWrite(StringFormat("Elapsed time to transform XML to JSON: %.3f seconds", TimerDiff($hTimer) / 1000) & @CRLF) ;Pretty-print JSON $sCmdOutput = _jqPrettyPrintJson($sJson, " ") If @error Then MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Pretty-print failed - @error = " & @error & " @extended = " & @extended) ConsoleWrite("ERROR:" & @CRLF & $sCmdOutput & @CRLF) Exit EndIf ConsoleWrite($sCmdOutput & @CRLF) EndFunc Output Elapsed time to transform XML to JSON: 0.003 seconds { "catalog": { "book": [ { "id": "bk101", "author": "Gambardella, Matthew", "title": "XML Developer's Guide", "genre": "Computer", "price": "44.95", "publish_date": "2000-10-01", "description": "An in-depth look at creating applications with XML." }, { "id": "bk102", "author": "Ralls, Kim", "title": "Midnight Rain", "genre": "Fantasy", "price": "5.95", "publish_date": "2000-12-16", "description": "A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world." }, { "id": "bk103", "author": "Corets, Eva", "title": "Maeve Ascendant", "genre": "Fantasy", "price": "5.95", "publish_date": "2000-11-17", "description": "After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society." }, { "id": "bk104", "author": "Corets, Eva", "title": "Oberon's Legacy", "genre": "Fantasy", "price": "5.95", "publish_date": "2001-03-10", "description": "In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant." }, { "id": "bk105", "author": "Corets, Eva", "title": "The Sundered Grail", "genre": "Fantasy", "price": "5.95", "publish_date": "2001-09-10", "description": "The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy." }, { "id": "bk106", "author": "Randall, Cynthia", "title": "Lover Birds", "genre": "Romance", "price": "4.95", "publish_date": "2000-09-02", "description": "When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled." }, { "id": "bk107", "author": "Thurman, Paula", "title": "Splish Splash", "genre": "Romance", "price": "4.95", "publish_date": "2000-11-02", "description": "A deep sea diver finds true love twenty thousand leagues beneath the sea." }, { "id": "bk108", "author": "Knorr, Stefan", "title": "Creepy Crawlies", "genre": "Horror", "price": "4.95", "publish_date": "2000-12-06", "description": "An anthology of horror stories about roaches, centipedes, scorpions and other insects." }, { "id": "bk109", "author": "Kress, Peter", "title": "Paradox Lost", "genre": "Science Fiction", "price": "6.95", "publish_date": "2000-11-02", "description": "After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum." }, { "id": "bk110", "author": "O'Brien, Tim", "title": "Microsoft .NET: The Programming Bible", "genre": "Computer", "price": "36.95", "publish_date": "2000-12-09", "description": "Microsoft's .NET initiative is explored in detail in this deep programmer's reference." }, { "id": "bk111", "author": "O'Brien, Tim", "title": "MSXML3: A Comprehensive Guide", "genre": "Computer", "price": "36.95", "publish_date": "2000-12-01", "description": "The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more." }, { "id": "bk112", "author": "Galos, Mike", "title": "Visual Studio 7: A Comprehensive Guide", "genre": "Computer", "price": "49.95", "publish_date": "2001-04-16", "description": "Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment." } ] } } Xml2Json UDF (2022-12-07).zip2 points -
If I offended with something, I apologize - maybe it's translation or maybe Asperger's spectrum (mine :)). Thank you so much for your help, now I just want to go further on my own - without wasting your time. And this is possible thanks to you! Thank you and I hope that if I encounter problems I will get your help in the future too!1 point
-
Suggested modification to ArrayDisplayInternals.au3
argumentum reacted to Tippex for a topic
Also missing from the attachment Posted March 20, 2019 ? Thank you. I have updated my attachment in my previous post.1 point -
Thanks a lot for your quick help. It works with function "_WD_LinkClickByText". I need to learn more on your recommendation "console output". I'm newbie with webdriver and Autoit. I found this post which had similar with my problem and no final conclusion. Next time, I will open new thread.1 point
-
This seems to be a general flaw in AutoIt3Wrapper for a long long time. Need to think what the best solution for this is for the GUI to ensure it shows the actual content in stead of its translated value. I will not make other changes to comments or add extra fields as that is something you could do yourself via a directive.1 point
-
Help File/Documentation Issues. (Discussion Only)
pixelsearch reacted to argumentum for a topic
so use: #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WinAPIGdi.au3> Example() Func Example() Local $sFontFile = StringLeft(@AutoItExe, StringInStr(@AutoItExe, '\', 0, -1) -1) & '\Examples\Helpfile\Extras\SF Square Head Bold.ttf' Local $hGUI = GUICreate("GDI", 720, 235) Local $idButton = GUICtrlCreateButton("AutoIt" & @CRLF & "forever", 100, 32, 520, 170, $BS_MULTILINE) _WinAPI_AddFontResourceEx($sFontFile, $FR_PRIVATE) GUICtrlSetFont($idButton, 28, 0, 0, "SF Square Head Bold") GUISetState() ; Loop until user exits Do Until GUIGetMsg() = $GUI_EVENT_CLOSE ; Clean up resources ; If not removed font is installed until system restart _WinAPI_RemoveFontResourceEx($sFontFile, $FR_PRIVATE) GUIDelete($hGUI) EndFunc ;==>Example for _WinAPI_AddFontResourceEx() and _WinAPI_RemoveFontResourceEx() and amend the remarks. Ok, sounds good1 point -
I don't know of a way to change the timeout settings for the _InetGetSource() function itself. However, if I needed control over HTTP request timeouts, I would most likely use the WinHTTPRequest object instead of _InetGetSource(). Specifically, I would use the SetTimeouts method of the object to set the desired timeouts. As you can see HERE, there's more than 1 timeout for a request. The SetTimeouts method allows you to set them as you desire. It would look something like this: ... $oHttp.SetTimeouts(5000, 5000, 5000, 5000) ...1 point
-
You're welcome. Here's a little hint, if you are truly interested in learning how to do it the way I suggested, then use your favorite web search engine and search for command line tools that will convert XML to JSON. When / If you find one that you like, start the scripting cycle of "trial -> error -> understanding" and see if you can figure out a solution that works for you. Of course doing it the way I suggested will require that you also learn how to write a simple jq filter to create the a TSV list from the JSON data. If you come to a point where you absolutely cannot proceed further without help, then come back, show your best effort or closest solution, explain where or how it is coming up short, and ask for assistance in understanding how to get past whatever obstacle you are encountering. I hope that helps. 😉 For the record, I was able to find an XML to JSON command line utility that works quite well and is only about 15K in size. After adding the conversion step to the previous script, I got the following times: Elapsed time to convert XML to JSON: 0.092 seconds Elapsed time to create TSV data: 0.435 seconds Elapsed time to create AutoIt array: 0.460 seconds As you can see, even with the conversion process, your XML file can still be converted into an AutoIt array in well under 1 second by using 2 command line tools (1 to convert XML to JSON and 1 to process the JSON and create a TSV file).1 point
-
Are you able to retrieve the data in any format other than XML, like JSON, CSV, or TSV? I'm not sure what you consider "instant", but the example below was able to convert the test02.json data to TSV data in 0.349 seconds and then convert the TSV data to an AutoIt array in an additional 0.026 seconds. That's a total elapsed time of 0.375 seconds. That elapsed time is certainly better than "a few seconds". I converted the test02.xml to test02.json using an online website (https://jsonformatter.org/xml-formatter). So the script below is an example that assumes you can either get your data as JSON or that you can/will convert your XML to JSON files before processing them. If you can get your data in CSV or TSV formats, then there are ways to convert it just as quickly as the JSON example. For reference, the script in your original post took about 3.5 seconds to run on the PC in which I ran the example script below. Note: For simplicity, the columns that may contain multiple values, like CPU, GPU, and SCList, were created as a list of "|"-separated values instead of arrays. You can use a function like StringSplit() or StringRegExp() to parse out individual values. Example #AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d #include <Constants.au3> #include <MyIncludes\jq\jq.au3> #include <Array.au3> example() Func example() Const $JSON_FILE = "test02.json" Local $hTimer = -1 Local $sCmdOutput = "" Local $aResult[0][6] ;The 2 lines below are only needed if the jq executale is not in the PATH or @ScriptDir _jqInit("C:\Utils\JQ\jq-win64.exe") If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Unable to find jq executable - @error = " & @error) ;Execute jq filter: Parse result into a tab-separated variable (TSV) list $hTimer = TimerInit() $sCmdOutput = _jqExecFile($JSON_FILE, "", '-r -f json_to_tsv.jq') If @error Then MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "jqExec failed - @error = " & @error & " @extended = " & @extended) ConsoleWrite("ERROR:" & @CRLF & $sCmdOutput & @CRLF) Exit EndIf ConsoleWrite(StringFormat("Elapsed time to create TSV data: %.3f seconds", TimerDiff($hTimer) / 1000) & @CRLF) ;Load TSV result into an array and display it _ArrayAdd($aResult, $sCmdOutput, 0, @TAB, @CRLF) ConsoleWrite(StringFormat("Elapsed time to create AutoIt array: %.3f seconds", TimerDiff($hTimer) / 1000) & @CRLF) _ArrayDisplay($aResult, "Parsed JSON Data", "", 0, Default, "ID|PName|PExt|CPU|GPU|SCList") EndFunc Output Elapsed time to create TSV data: 0.349 seconds Elapsed time to create AutoIt array: 0.375 seconds Example Array test02.json1 point
-
A very rudimentary flowchart example. It will be a lot of effort to make this more functional and defining the node pictures will be a lot of work to define. #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <ColorConstants.au3> Global Const $g_MAXGr = 8 Global $g_aidGraphics[$g_MAXGr + 1] ; 0 and $g_MAXGr entries not used to allow GUICtrlDelete result Global $g_idDel, $g_hcanvas Example() Func Example() Local $idMsg, $iInc, $i GUICreate("My flowchart tool", -1, -1, 100, 100) Local $idDel1 = GUICtrlCreateButton("Open canvas", 50, 200, 150) GUISetState(@SW_SHOW) Createcanvas() $i = 1 $iInc = 1 ;$i=5 ; uncomment to delete starting from last define Graphic control ;$iInc=-1 Do $idMsg = GUIGetMsg() If $idMsg = $idDel1 Then $i = Create($iInc) If $idMsg = $g_idDel Then GUICtrlDelete($g_aidGraphics[$i]) $i = $i + $iInc If $i < 0 Or $i > $g_MAXGr Then Exit EndIf Until $idMsg = $GUI_EVENT_CLOSE EndFunc ;==>Example Func Create($iInc) GUIDelete($g_hcanvas) Createcanvas() If $iInc = -1 Then Return 5 Return 1 EndFunc ;==>Create func drawStartOrEnd($x,$y,$r) local $graphic = GUICtrlCreateGraphic(20, 50, 100, 100) GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $COLOR_BLACK, $COLOR_BLACK) GUICtrlSetGraphic(-1, $GUI_GR_PIE, $x, $y, $r, 0, 360) ;~ circle return $graphic EndFunc func drawProcess($x,$y) $graphic = GUICtrlCreateGraphic(20, 50, 100, 100) GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $COLOR_BLACK, $COLOR_BLACK) GUICtrlSetGraphic(-1, $GUI_GR_RECT, $x, $y, 60, 20) ;~ rectangle return $graphic EndFunc func drawDiamond($x,$y, $w, $h) $graphic = GUICtrlCreateGraphic(20, 50, 100, 100) GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $COLOR_BLACK, $COLOR_BLACK) GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $x , $y+ round($h/2)) GUICtrlSetGraphic(-1, $GUI_GR_LINE, $x+20,$y) GUICtrlSetGraphic(-1, $GUI_GR_LINE, $x+40,$y+ round($h/2)) GUICtrlSetGraphic(-1, $GUI_GR_LINE, $x+20,$y+$h) GUICtrlSetGraphic(-1, $GUI_GR_LINE, $x,$y+round($h/2)) return $graphic EndFunc func drawConnector($x1, $y1, $x2, $y2) $graphic = GUICtrlCreateGraphic(20, 50, 100, 100) GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $COLOR_BLACK, $COLOR_BLACK) GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $x1 , $y1) GUICtrlSetGraphic(-1, $GUI_GR_LINE, $x2, $y2) return $graphic EndFunc Func Createcanvas() ;~ If in future we want to drawtoPrinter, drawToVisio, drawToPpt is use variable functions $drawStartOrEnd = drawStartOrEnd $drawProcess = drawProcess $drawDiamond = drawDiamond $drawConnector = drawConnector $g_hcanvas = GUICreate("My canvas",640, 480) $g_idDel = GUICtrlCreateButton("Delete", 50, 165, 50) local $w=120 local $h=40 $g_aidGraphics[1]=$drawStartOrEnd(200,10,$w/9) $g_aidGraphics[2]=$drawProcess(170, 20+$h) $g_aidGraphics[3]=$drawDiamond(180, 30+2*$h,$w,$h) $g_aidGraphics[4]=$drawStartOrEnd(200,60+3*$h,$w/9) $g_aidGraphics[5]=$drawConnector(200,10 , 200, 20+$h) $g_aidGraphics[6]=$drawConnector(200,20+$h , 200, 30+2*$h ) $g_aidGraphics[7]=$drawConnector(200,30+2*$h +$h , 200, 60+3*$h ) GUISetState(@SW_SHOW) EndFunc ;==>Createcanvas1 point
-
@Gianni: What is the problem? Do you need additional info on how MCF works under the hood?1 point