Popular Post TheXman Posted October 2, 2022 Popular Post Share Posted October 2, 2022 (edited) 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 ) expandcollapse popup#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).zip Edited December 7, 2022 by TheXman jugador, argumentum, lesmly and 4 others 4 3 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
littlebigman Posted November 14, 2023 Share Posted November 14, 2023 (edited) Is there a way to save data back to an XML file? Also, since, unlike XML, JSON doesn't have a notion of attributes, those are just another data in JSON: How can they be saved back as attributes in the XML output file? It's important because, for instance, GPS coordinates in GPX files must be attributes: <wpt lat="48.6443057" lon="2.7537863"><name>blah</name></wpt> Edited November 14, 2023 by littlebigman Link to comment Share on other sites More sharing options...
TheXman Posted November 14, 2023 Author Share Posted November 14, 2023 (edited) 6 hours ago, littlebigman said: Is there a way to save data back to an XML file? A quick query of "json2xml" or "JSON to XML" in your Internet search engine of choice would show that there are tools out there that convert JSON to XML (or at least try to). So the short answer is yes. However, in all the years that I've worked with XML and JSON, I've never tried to convert JSON to XML. So I'm not sure how well any of those conversion tools work - especially when it comes to XML datasets that make extensive use of things like CDATA and namespaces. If I wanted to create or modify an XML dataset, I would use tools & languages designed for that purpose - like working with the XML DOM using XPATH or transforming it using XSLT. I always try use the best tools for the job because it makes the job so much easier. I pretty much answered the same question in your previous topic, near the bottom of this post: Edited November 14, 2023 by TheXman Corrected typo Musashi 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
littlebigman Posted November 14, 2023 Share Posted November 14, 2023 Thanks. I was curious about that UDF because of "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", but it looks like it's not a good idea if outputting data back to XML is required. Link to comment Share on other sites More sharing options...
water Posted November 14, 2023 Share Posted November 14, 2023 @TheXman I added your UDF to the wiki My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
TheXman Posted November 14, 2023 Author Share Posted November 14, 2023 Thanks @water CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman 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