Sign in to follow this  
Followers 0
Terenz

[JS] JSON to XML

2 posts in this topic

#1 ·  Posted (edited)

After thinking about it, i prefer to use XML instead of the original JSON. XML is a language and not a data format like JSON and i know how to manage. Since our "server manager" refuse to do the conversion i need to do by myself, well "by myself" is a big word since i have many problem to do.

There isn't any example of JSON->XML on the forum ( except a proof of concept ) and the only language i have found is Javascript so for avoid any IE or dipendecy i'm here to convert in autoit, i hope.

The author, there is the explaination of the "rules" he used

http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html

/*  This work is licensed under Creative Commons GNU LGPL License.

    License: http://creativecommons.org/licenses/LGPL/2.1/
   Version: 0.9
    Author:  Stefan Goessner/2006
    Web:     http://goessner.net/ 
*/
function json2xml(o, tab) {
   var toXml = function(v, name, ind) {
      var xml = "";
      if (v instanceof Array) {
         for (var i=0, n=v.length; i<n; i++)
            xml += ind + toXml(v[i], name, ind+"\t") + "\n";
      }
      else if (typeof(v) == "object") {
         var hasChild = false;
         xml += ind + "<" + name;
         for (var m in v) {
            if (m.charAt(0) == "@")
               xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
            else
               hasChild = true;
         }
         xml += hasChild ? ">" : "/>";
         if (hasChild) {
            for (var m in v) {
               if (m == "#text")
                  xml += v[m];
               else if (m == "#cdata")
                  xml += "<![CDATA[" + v[m] + "]]>";
               else if (m.charAt(0) != "@")
                  xml += toXml(v[m], m, ind+"\t");
            }
            xml += (xml.charAt(xml.length-1)=="\n"?ind:"") + "</" + name + ">";
         }
      }
      else {
         xml += ind + "<" + name + ">" + v.toString() +  "</" + name + ">";
      }
      return xml;
   }, xml="";
   for (var m in o)
      xml += toXml(o[m], m, "");
   return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
}

And example of JSON if need:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

If the next question is what you have do...i have do but too much things of JS are unclear. This was my best attempt

If $hasChild Then
    $xml += ">"
Else
    $xml += "/>"
EndIf
If $hasChild Then
;~  For (var m In v) {
        If $m = "#text" Then
            $xml += $v[$m]
        ElseIf $m = "#cdata" Then
            $xml += "<![CDATA[" + $v[$m] + "]]>"
        ElseIf StringLeft($m, 1) <> "@" Then ; 'm.charAt(0)'
            $xml += ;toXml(v[m], m, ind + "\t")
        EndIf
        $xml += ;($xml.charAt(xml.length - 1) == "\n" ? ind : "") + "</" + name + ">";
;~  Next
EndIf

I think can be useful also for other in the same situation. Thanks

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites



You can just use ScriptControl to use Javascript with AutoIt

$oSC = ObjCreate("ScriptControl")
$oSC.Language = "JScript"

$oSC.AddCode(FileRead("js.txt"))
$json2xml = $oSC.Eval("json2xml")
$sXML = $json2xml($oSC.Eval("("&FileRead("json.txt")&")"), "")
MsgBox(0, "", VarGetType($sXML)&@CRLF&$sXML)

In the example above, i simply saved your javascript function as "js.txt" and the example of JSON as "json.txt"

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0