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
*/
// ===================================
/* xml2var ( _xml )__LITBR__by Andries Heylen and Fabrice Nordmann__LITBR____LITBR__v.1.2 - Feb 12, 2009__LITBR__ - bug fixed if a node contains several quotes__LITBR__v.1.1 - Feb 3, 2009__LITBR__ - adds a second feed-back ($xml2varNames) with just the list of declared varialbles__LITBR__ - cleans up the place after process__LITBR__v.1.0 - Nov 8, 2008__LITBR__ - adds feed back as a result of function__LITBR__ - evaluates expressions starting with = (use of xmlGet)__LITBR__v.ß1 - Nov 2008__LITBR____LITBR____LITBR__Declares local variables from an xml structure__LITBR____LITBR__Requires xmlSet and xmlGet__LITBR____LITBR__*/__LITBR____LITBR__Case ( not IsEmpty ( Substitute ( Trim ( _xml ) ; ¶ ; "" ))__LITBR____LITBR__;__LITBR__Let ([ __LITBR__ _xml = Right ( _xml ; Length ( _xml ) - Position ( _xml ; "<" ; 1 ; 1 ) + 1 )__LITBR__; _varName = Substitute ( Trim ( _xml ) ; ¶ ; "" )__LITBR__; $varName = Substitute ( Left ( _varName ; Position ( _varName ; ">" ; 1 ; 1 ) -1 ) ; "<" ; "" )__LITBR__; _varValue_raw = xmlGet ( _xml ; $varName )__LITBR__; $varValue = Quote ( _varValue_raw )__LITBR__; _tag = xmlSet ( $varName ; $varValue )__LITBR__; _tag_raw = xmlSet ( $varName ; _varValue_raw )__LITBR__; _next = Replace ( _xml ; 1 ; Length ( _tag_raw ) ; "" )__LITBR__; $$xml2varCounter = $$xml2varCounter + GetAsBoolean ( Length ( $varValue ) > 2 ) // 2 for the quotes__LITBR__; $$xml2varNames = List ( $$xml2varNames ; $varName )__LITBR__; $$xml2varList = List ( $$xml2varList ; TextColor ( "$" & $varName ; RGB ( 40 ; 255 ; 90 )) & " = " & TextColor ( $varValue ; RGB ( 0 ; 0 ; 255 )))__LITBR____LITBR__];__LITBR__Evaluate ( "let ( $" & $varName & "=" & $varValue & " ; \"\" )" ) // TO TEST THIS FUNCTION, SIMPLY ADD A $ SIGN TO OBSERVE GLOBAL VARIABLES__LITBR__& xml2var ( _next )__LITBR__)__LITBR____LITBR__; Let ([ __LITBR__ $xml2varFeedBack = List ( TextStyleAdd ( GetAsNumber ( $$xml2varCounter ) & " variables declared." ; Underline ) ; $$xml2varList ) __LITBR__; $$xml2varCounter = "" __LITBR__; $$xml2varList = "" __LITBR__; $xml2varNames = $$xml2varNames__LITBR__; $$xml2varNames = ""__LITBR__; $varName = ""__LITBR__; $varValue = ""__LITBR____LITBR__]; __LITBR____LITBR__$xml2varFeedBack )__LITBR__)__LITBR____LITBR__// ===================================__LITBR__/*__LITBR____LITBR__ This function is published on FileMaker Custom Functions__LITBR__ to check for updates and provide feedback and bug reports__LITBR__ please visit http://www.fmfunctions.com/fid/122__LITBR____LITBR__ Prototype: xml2var( _xml )__LITBR__ Function Author: Fabrice (http://www.fmfunctions.com/mid/37)__LITBR__ Last updated: 14 May 2009__LITBR__ Version: 1.3__LITBR____LITBR__*/__LITBR__// ===================================
Login or register to comment
Create a new account with fmcustomfunctions.com or login to post a comment.
Comments
07 December 2008
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.
07 December 2008
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 )
07 December 2008
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 )
07 December 2008
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 )