« MBS FileMaker Advent … | Home | MBS FileMaker Advent … »

MBS FileMaker Advent calendar - Door 14 - XML

candy cane Monkeybread Monkey as an elf candy cane
Day 14 - XML

When rebuilding the weather station, you have probably already seen that you can get the data not only as JSON, but also as XML.

XML stands for eXtensible Markup Language and is a markup language that was developed to store and transport data in a structured and easily readable form. It was standardized by the World Wide Web Consortium (W3C) and first published in 1998. Similar to JSON, XML is often used to exchange data between interfaces. Like JSON, XML is also structured in layers, but the way it is written is more reminiscent of HTML. This is because in XML we also work with tags.

We still remember our JSON example that looked like this:

{
  "person": [
    {
      "Name": "Smith",
      "FirstName": "Toni",
      "Hobbies": ["programming", "reading", "taking photos"],
      "Dog": {
        "name": "Benni",
        "age": 6,
        "LikeTreats": true
      }
    },
    {
      "Name": "Doe",
      "FirstName": "Jane",
      "Hobbies": ["dancing", "karate", "writing"],
      "Dog": {
        "name": "Nala",
        "age": 2,
        "LikeTreats": true
      }
    }
  ]
}

If we now wanted to represent this JSON as XML, it would look like this, for example:

<persons> 
	<person> 
		<Name>Smith</Name> 
		<FirstName>Toni</FirstName> 
		<Hobbies>
			<Hobby>programming</Hobby> 
			<Hobby>reading</Hobby> 
			<Hobby>taking photos</Hobby> 
		</Hobbies> 
		<Dog>
			<name>Benni</name> 
			<age>6</age> 
			<LikeTreats>true</LikeTreats>
		 </Dog> 
	</person> 
	<person>
		<Name>Doe</Name> 
		<FirstName>Jane</FirstName> 
		<Hobbies> 
			<Hobby>dancing</Hobby> 
			<Hobby>karate</Hobby>
			<Hobby>writing</Hobby> 
		</Hobbies> 
		<Dog> 
			<name>Nala</name> 
			<age>2</age> 
			<LikeTreats>true</LikeTreats>
		</Dog> 
	</person> 
</persons>

We have a root tag in this case Persons. This is the first level that encloses the data. Then we can specify the individual persons. In JSON, the persons have an array. In XML, the array is replaced by repeating a tag several times. So that we have a valid JSON, every tag that is opened must also be closed again. The situation is similar for nested objects such as the dog. Here, too, we have an extra hierarchy for the dog, which is then nested again.

Another difference to JSON is that XML can have attributes. In XML, you can store information both as the content of an element and as attributes in a tag. Example:

<Person name="Smith" age="30">
    <Hobby>reading</Hobby>
</Person>

Here, the <person> tag has two attributes: name and age.

To summarize, arrays need to be represented in XML by repetition, and objects by nested tags. There are also attributes that we can use. Otherwise, the main idea between JSON and XML remains the same: data is represented in a structured way, just with a different syntax. JSON is a lot more compact and often easier to read for the developer, but many APIs use XML instead of JSON. XML is also used for e-invoices, for example, and is embedded in the PDF for the ZUGFeRD standard, enabling invoices to be processed automatically. XML remains essential, especially where strict structuring and validation of data is required.

The MBS FileMaker Plugin also offers you functions with which you can create, read and change XML. Since we are already a little familiar with our weather API, today we want to retrieve the same data with XML.

The address we enter in the browser is almost the same as for JSON, except that we add a mode=xml to specify that we want to retrieve the data as XML. This is what the retrieval on the web now can looks like in our script:

Insert from URL [ Select ; With dialog: Off ; Target: $XML ; 
	"https://api.openweathermap.org/data/2.5/weather?q=" & 
	Giftee::City & "," & Giftee::State & "," & Giftee::Country & "
	&appid=" & $$APIKey & "&mode=xml" ]

The XML that comes out for Miami can then look like this, for example:


<?xml version="1.0" encoding="UTF-8"?>
<current>
	<city id="4164138" name=„Miami">
		<coord lon="-80.1937" lat=„25.7743"></coord>
		<country>US</country>
		<timezone>-18000</timezone>
		<sun rise="2024-11-28T11:48:46" set=„2024-11-28T22:29:29“></sun>
	</city>
	<temperature value="295.59" min="294.15" max="296.47" unit=„kelvin"></temperature>
	<feels_like value="296.19" unit=„kelvin"></feels_like>
	<humidity value="88" unit=„%"></humidity>
	<pressure value="1016" unit=„hPa"></pressure>
	<wind>
		<speed value="1.54" unit="m/s" name=„Calm"></speed>
		<gusts></gusts>
		<direction value="120" code="ESE" name=„East-southeast"></direction>
	</wind>
	<clouds value="20" name="few clouds“></clouds>
	<visibility value=„10000"></visibility>
	<precipitation mode=„no"></precipitation>
	<weather number="801" value="few clouds" icon=„02n“></weather>
	<lastupdate value=„2024-11-29T02:38:49"></lastupdate>
</current>

Here we can see that attributes are used extensively. We need the attribute value from the temperature part and the weather part. From the weather part, we also need the attributes value and icon.

Again, we have to make our way through the XML structure and this looks similar to the JSON. We separate the individual levels with dots here too. If we then want to read an attribute, we enter # to make it clear that we want an attribute, followed by the name of the attribute. In this way, we can access the information using the following paths:

Temperature current.temperature#value
Weather description current.weather#value
Weather icon current.weather#icon

We now pass these paths to the XML.GetPathValue function. This reads out the value by specifying the XML and the path. In our case, it looks like this.

Set Variable [ $Temp ; Value: MBS("XML.GetPathValue"; $XML; "current.temperature#value") ]
Set Variable [ $description ; Value: MBS("XML.GetPathValue"; $XML; "current.weather#value") ]
Set Variable [ $icon ; Value: MBS("XML.GetPathValue"; $XML; "current.weather#icon") ]

We have already written the script icon for converting the icon code into an emojie and use it again here. We then set the values in the appropriate fields.

Perform Script [ Specified: From list ; "Icon" ; Parameter: $icon ]
Set Variable [ $icon ; Value: Get(ScriptResult) ]
Set Field [ Giftee::Weather_temperature ; $Temp ]
Set Field [ Giftee::Weather_weather ; $description ]
Set Field [ Giftee::Weather_icon ; $icon ]

I hope you enjoyed this little door and that we will read each other again tomorrow.


Monkeybread Software Logo with Monkey with Santa hat
13 👈 14 of 24 👉 15
Claris FileMaker Plugin
14 12 24 - 17:50