Tech Tip: Utility Methods to Convert 4D Object to XML
PRODUCT: 4D | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: May 8, 2015
Below is a method that will convert a 4D Object,which contains data in a JSON, to XML format.
There are two methods involved, one will convert the data to an XML reference by parsing through the object via recursion. The other method handles the setup of the XML reference and takes in an object as a passed parameter and outputs the XML reference, which is in Text format.
//============================================================= // Method: OBtoXMLParse // Description: Goes through an Object and adds // the values to an XML Reference // --------------------------------------------------------- // Parameters: // $1 - XML Refernce that will contain the converted data // $2 - Object that will get converted // $3 - The xPath for the XML //============================================================= C_TEXT($1;$xmlRef_p) C_OBJECT($2;$objects_o) C_TEXT($3;$xpath_t) ARRAY TEXT($arrProp_at;0) C_TEXT($elemRef_t) C_LONGINT($countA;$countB) If (Count parameters=3) $xmlRef_p:=$1 $objects_o:=$2 $xpath_t:=$3 OB GET PROPERTY NAMES($objects_o;$arrProp_at) For ($countA;1;Size of array($arrProp_at)) $type_l:=OB Get type($objects_o;$arrProp_at{$countA}) Case of : (($type_l=Is real) | ($type_l=Is boolean) | ($type_l=Is string var) | ($type_l=Is text)) $elemRef_t:=DOM Create XML element($xmlRef_p;$xpath_t+"/"+$arrProp_at{$countA}) DOM SET XML ELEMENT VALUE($elemRef_t;OB Get($objects_o;$arrProp_at{$countA})) : ($type_l=Is object) $elemRef_t:=DOM Create XML element($xmlRef_p;$xpath_t+"/"+$arrProp_at{$countA}) C_OBJECT($temp_o) $temp_o:=OB Get($objects_o;$arrProp_at{$countA}) OBtoXmlParse($xmlRef_p;$temp_o;$xpath_t+"/"+$arrProp_at{$countA}) : ($type_l=Object array) ARRAY OBJECT($value_ao;0) OB GET ARRAY($objects_o;$arrProp_at{$countA};$value_ao) For ($countB;1;Size of array($value_ao)) $elemRef_t:=DOM Create XML element($xmlRef_p;$xpath_t+"/"+$arrProp_at{$countA}) OBtoXmlParse ($xmlRef_p;$value_ao{$countB};$xpath_t+"/"+$arrProp_at{$countA}+"["+String($countB)+"]") End for : (($type_l=Is JSON null) | ($type_l=Is undefined)) $elemRef_t:=DOM Create XML element($xmlRef_p;$xpath_t+"/"+$arrProp_at{$countA}) DOM SET XML ELEMENT VALUE($elemRef_t;"") End case End for $1:=$xmlRef_p End if |
//============================================================= // Method: OBtoXML // Description: Converts an Object type to XML // and outputs the XML Reference // WARNING: XML must be closed manually, // not doing so can lead to a memory leak // --------------------------------------------------------- // Parameters: // $1 - Object that will get converted // // Output: // $0 - The XML reference //============================================================= C_TEXT($0;$xmlRef_t) C_OBJECT($1;$object_o) C_TEXT($xpath_t) If (Count parameters=1) $object_o:=$1 $xpath_t:="root" $xmlRef_t:=DOM Create XML Ref($xpath_t) OBtoXmlParse ($xmlRef_t;$object_o;$xpath_t) $0:=$xmlRef_t Else $0:="" End if |
To use the method, create a text variable to contain the XML reference and assign it to the OBtoXML method with an object passed as the parameter. Below is an example:
//Building a Sample Object: C_OBJECT($sample_o;$ref_smith;$ref_richard;$ref_susan;$ref_james) ARRAY OBJECT($arrChildren;0) OB SET($ref_smith;"LastName";"Smith";"FirstName";"John";"age";42;"client";True) OB SET($ref_richard;"name";"Richard";"age";7) APPEND TO ARRAY($arrChildren;$ref_richard) OB SET($ref_susan;"name";"Susan";"age";4) APPEND TO ARRAY($arrChildren;$ref_susan) OB SET($ref_james;"name";"James";"age";3) APPEND TO ARRAY($arrChildren;$ref_james) OB SET ARRAY($ref_smith;"Children";$arrChildren) OB SET($sample_o;"Parent";$ref_smith) //Testing the utility method: C_TEXT($xmlRef_t) $xmlRef_t:=OBToXML($sample_o) DOM EXPORT TO FILE($xmlRef_t;"") DOM CLOSE XML($xmlRef_t) |
Below is the Sample object in $sample_o
{ "root": { "Parent": { "LastName": "Smith", "FirstName": "John", "age": "42", "client": "true", "Children": [ { "name": "Richard", "age": "7" }, { "name": "Susan", "age": "4" }, { "name": "James", "age": "3" } ] } } } |
Below is the resulting XML:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <root> <Parent> <LastName>Smith</LastName> <FirstName>John</FirstName> <age>42</age> <client>true</client> <Children> <name>Richard</name> <age>7</age> </Children> <Children> <name>Susan <age>4 </Children> <Children> <name>James <age>3 </Children> </Parent> </root> |