xml2var ( )

Function stats

Average user rating
4.5000
37
122
9999
Support
FileMaker 8.0 +
Date posted
08 December 2008
Last updated
22 July 2014
Version
Recursive function
Yes

Author Info
 Fabrice

74 functions

Average Rating 4.4

author_avatar



 

Function overview

Prototype

xml2var  ( _xml )


Parameters

_xml  


Description

Tags:  xml   Variables   Debug  

Declares local variables from an xml structure.

Useful for those who use xmlSet to send script parameters and want to parse the whole parameter without declaring each variable.

Examples

Sample input

<firstName>John</firstName>
<lastName>Smith</lastName>


Sample output

$firstName = John
$lastName = Smith

 

Function code

/* xml2var ( _xml )

by Fabrice Nordmann - 1-more-thing -
http://www.1-more-thing.com
Twitter : 1morethingtweet

and Andries Heylen - BH&A
http://www.bh-a.com
Twitter : AndriesHeylen

v2.1 - Jul, 2014
- handles Char ( 1000013 ), returned by Base64encode (FM13)
v2 - Jun, 2013
- implicit type based on naming convention
    $var.n    -> number (also implicit if the value is a number and no other suffix is passed)
    $var.d    -> date
    $var.t    -> time
    $var.ts    -> timestamp
    $var.b    -> boolean
    $var.txt    -> text (default)

v.1.2 - Feb 12, 2009
- bug fixed if a node contains several quotes

v.1.1 - Feb 3, 2009
- adds a second feed-back ($xml2varNames) with just the list of declared varialbles
- cleans up the place after process

v.1.0 - Nov 8, 2008
- adds feed back as a result of function
- evaluates expressions starting with = (use of xmlGet)

v.ß1 - Nov 2008

Declares local variables from an xml structure
Requires xmlSet and xmlGet

*/



Case ( not IsEmpty ( Substitute ( Trim ( _xml ) ; ¶ ; "" ));

Let ([
    _xml = Substitute ( Right ( _xml ; Length ( _xml ) - Position ( _xml ; "<" ; 1 ; 1 ) + 1 ); Char ( 1000013 ) ; "#CR#" ) ;
    _varName = Substitute ( Trim ( _xml ) ; ¶ ; "" );
    $varName = Substitute ( Left ( _varName ; Position ( _varName ; ">" ; 1 ; 1 ) -1 ) ; "<" ; "" );
    _varValue_raw = xmlGet ( _xml ; $varName ) ;
    _varType = Case (
                Right ( $varName ; 2 ) = ".n" ; "Number" ;
                Right ( $varName ; 2 ) = ".d" ; "Date" ;
                Right ( $varName ; 2 ) = ".t" ; "Time" ;
                Right ( $varName ; 3 ) = ".ts" ; "TimeStamp" ;
                Right ( $varName ; 4 ) = ".txt" ; "Text" ;
                Right ( $varName ; 5 ) = ".list" ; "Text" ;
                Right ( $varName ; 2 ) = ".b" ; "Boolean" ;
                _varValue_raw = GetAsNumber ( _varValue_raw ) and not IsEmpty ( _varValue_raw ) ; "Number" ;
                "Text"
                );
    $varValue = Substitute ( Case ( not IsEmpty ( _varType ) ; "GetAs" & _varType & "(" & Case ( _varType = "Number" ; _varValue_raw ; Quote ( _varValue_raw )) & ")" ; Quote ( _varValue_raw )) ; "#CR#" ; Char ( 1000013 ));
    _tag = xmlSet ( $varName ; $varValue );
    _tag_raw = xmlSet ( $varName ; _varValue_raw );
    _next = Replace ( _xml ; 1 ; Length ( _tag_raw ) ; "" );
    $$xml2varCounter = $$xml2varCounter + GetAsBoolean ( Length ( $varValue ) > 2 ); // 2 for the quotes
    $$xml2varNames = List ( $$xml2varNames ; $varName );
    $$xml2varList = List ( $$xml2varList ; TextColor ( "$" & $varName ; RGB ( 40 ; 255 ; 90 )) & " = " & TextColor ( $varValue ; RGB ( 0 ; 0 ; 255 )))
];
Evaluate ( "let ( $" & $varName & "=" & $varValue & " ; \"\" )" ) // TO TEST THIS FUNCTION, SIMPLY ADD A $ SIGN TO OBSERVE GLOBAL VARIABLES

& xml2var ( _next )

)

; Let ([

$xml2varFeedBack = List ( TextStyleAdd ( GetAsNumber ( 0+ $$xml2varCounter ) & " variables declared." ; Underline ) ; $$xml2varList )
; $$xml2varCounter = ""
; $$xml2varList = ""
; $xml2varNames = $$xml2varNames
; $$xml2varNames = ""
; $varName = ""
; $varValue = ""
];
$xml2varFeedBack )
)

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/122

    Prototype: xml2var( _xml )
    Function Author: Fabrice (http://www.fmfunctions.com/mid/37)
    Last updated: 22 July 2014
    Version: 2.2

*/
// ===================================

 

Comments

Genx
08 December 2008



Fabrice,
This doesn't really have anything to do with the function, but just out of curiosity, what sort of XML are you actually parsing with FileMaker / for what purpose.
Cheers.
  General comment
Fabrice
08 December 2008



Hi Alex,
well... I do everything with xml now : passing parameters, returning results, interacting with php...
Instead of having a script parameter like "Smith¶John¶10/5/1947" where you have to remember the parameter order and sometimes send an empty parameter ("Smith¶¶10/5/1947"), I like xml because it names the parameters, the order is not important, and you can embed tags (several invoices and each invoice has several items...)
To ease the work with xml (which is a little too verbose for me), I use xmlSet, xmlGet and xml2var.
Even simple parameters are passed this way, because even the simplest script might expect a second or a third parameter one day.
HTH,
Fabrice
(Edited by Fabrice on 10/12/08 )
  General comment
Genx
08 December 2008



So you basically use them as an equivalent to a name based key value array (other than the actual XML parsing abilities)? I was thinking of writing a few functions to do that, but if you've already done it, there's no point :)

Will definitely take a PROPER look at all 3 of these when i get a moment, I think it may solve a few problem's I've been having.
(Edited by Genx on 10/12/08 )
  General comment
Fabrice
08 December 2008



Exactly, it's just a named array, but multi-dimensional (nested).
I also add a comment to each of my scripts with a sample parameter such as :

xmlset ( "contact" ;
xmlset ( "firstname" ; "" ) // optional
& xmlset ( "lastname" ; "" ) // mandatory
& xmlset ( "isDefaultContact" ; 0 ) // boolean
)

Since FM 9 and the ability to open multiple script windows, it makes it a snap to just have a look at a script parameter, and copy/paste it to the right place.
(Edited by Fabrice on 10/12/08 )
  General comment
Andries Heylen
22 January 2013



You should replace
; _next = Replace ( _xml ; 1 ; Length ( _tag ) - 2 ; "" )
with
; _next = Replace ( _xml ; 1 ; Length ( _tag ) - PatternCount ( $varValue ; "\"" ) ; "" )

to deal with all the quotes :)
 Bug report
CVB
29 June 2014



Thanks for this great function. This combined with xmlSet have revolutionised my development. Might interest you to know that I have used this with executesql to do an advanced mail merge. I just xml2var the whole body of my message though I have had to adapt it so that variables are appended with the counter as I can have repeats in my text.
  General comment
Fabrice
29 June 2014



Thanks for the feed back. Good to know that you find it as useful as I do.
Indeed, the potential for code simplification is limitless.
  General comment