xml2var ( )

Function stats

Average user rating
4.5000
37
122
9999
Support
FileMaker 8.0 +
Date posted
07 December 2008
Last updated
14 May 2009
Version
Recursive function
Yes

Author Info
 Fabrice

39 functions

Average Rating 4.3

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 Andries Heylen and Fabrice Nordmann

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 = Right ( _xml ; Length ( _xml ) - Position ( _xml ; "<" ; 1 ; 1 ) + 1 )
; _varName = Substitute ( Trim ( _xml ) ; ¶ ; "" )
; $varName = Substitute ( Left ( _varName ; Position ( _varName ; ">" ; 1 ; 1 ) -1 ) ; "<" ; "" )
; _varValue_raw = xmlGet ( _xml ; $varName )
; $varValue = Quote ( _varValue_raw )
; _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 ( $$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: 14 May 2009
    Version: 1.3

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

 

Comments

Genx
07 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
07 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
07 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
07 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

 

 


Login or register to comment

Create a new account with fmcustomfunctions.com or login to post a comment.