Convert XML into variables

 

XMLs can be extremely useful when it comes to passing information to a script in a single pass parameter.

I know that many of my IT colleagues consider the XML format too loquacious and outdated.

This may be true when you see that to pass a single number, in many cases a long node name is used:

<ActualInvoiceNumberMainClient>123</ActualInvoiceNumberMainClient>

 

But for me the XML format is very readable and the data of the nodes can be easily extracted in Filemaker as they are simply lines of text.

Andy Knasinski has written one of my favorite custom functions for this:
ExtractData ( XML ; Attribute ; Instance )
You can find this function, as well as many other useful functions, on the great website of Brian Dunning.

Since I discovered this function it has become something like my Swiss Army Knife and I can’t count how many times I have used it.

Based on this custom function I wrote my own routine that creates local script variables from an XML.

This function is a recursive function and evaluates the content of the XML nodes and then creates local variables with the node names as variable names.

Please note that the function only evaluates nodes from the first level, which is sufficient for such transfers:

XMLconvertToVar

/*XMLconvertToVar ( XML ; Counter )
Example:
$XML = "<Karl>1</Karl>
<Stephan>2</Stephan>
<Lucas>3</Lucas>"

Call:
$dummy = XMLconvertToVar ( $XML ; 1 )

Result:
$Karl = 1
$Stephan = 2
$Lucas = 3

Counter MUST always be 1 when called.*/

Let (
[
max = PatternCount ( XML ; "</" ) ;
start = Position ( XML ; "</" ; 1 ; Counter ) + 2 ;
XMLShort = Middle ( XML ; start ; Length ( XML ) ) ;
tagLength = Position ( XMLShort ; ">" ; 1 ; 1 ) - 1 ;
name = Left ( XMLShort ; tagLength ) ;
tl = Left ( XML ; start + tagLength +1 ) ;
ct = PatternCount ( tl ; "</" & name & ">" ) ;
value = ( XML ; name ; 1 ) ;
result = Evaluate ("Let ($" & name & " = " & Quote ( value ) & " ;" & "\"\"" & ")" )
];
Wenn ( Counter ≤ max ; XMLconvertToVar ( XML ; Counter + 1 ) )
)

In the script I simply call the custom function with a dummy variable:

set variable [ $dummy ; value: XMLconvertToVar ( $XML ; 1 ) ] ]

 

Here’s the “While“ Version which is probably a little faster: 

XMLconvertToVar

/*XMLconvertToVar ( XML )
Example:
$XML = "<Karl>1</Karl>
<Stephan>2</Stephan>
<Lucas>3</Lucas>"

Call:
$dummy = XMLconvertToVar ( $XML )

Result:
$Karl = 1
$Stephan = 2
$Lucas = 3
*/

While ( [ counter = 1 ; max = PatternCount ( XML ; "</" ) ] ; counter ≤ max ; [ Start = Position ( XML ; "</" ; 1 ; counter ) + 2 ; XMLShort = Middle ( XML ; Start ; Length ( XML ) ) ; TagLength = Position ( XMLShort ; ">" ; 1 ; 1 ) - 1 ; name = Left ( XMLShort ; TagLength ) ; tl = Left ( XML ; Start + TagLength +1 ) ; ct = PatternCount ( tl ; "</" & name & ">" ) ; value = ExtractData ( XML ; name ; 1 ) ; result = Evaluate ("Let ($" & name & " = " & Quote ( value ) & " ;" & "\"\"" & ")" ) ; counter = counter + 1 ] ; counter )

This function is used in many of my databases when I need to pass multiple parameters in a script call.

It is easy to use, you can quickly add more parameters and saves you a lot of typing when applying the parameters.

You have any questions? Feel free to send me an email, I’ll try to answer as soon as possible.