In this tutorial we will read and inspect some XML data, more specifically we will examine
how to use feature paths and flattening to read an XML file.
This simple XML describes a building feature.
The building has address and location details it contains rooms and rooms contain walls.
We will use FME workbench to demonstrate how to work with feature paths and flattening.
Let's drag this XML file into a new workspace.
The format and data set in the ad reader dialogue automatically get populated.
Click on parameters.
How we set these reader parameters will directly affect how FME reads the XML.
There are three configuration types for the reader configuration.
If we had an xfmap or XRS file we could select the appropriate option and enter the file
location here.
XFmap or XRS files represent scripted configuration files for more advanced use, so let's focus
on feature paths configuration.
In order to read any given XML document you need to tell FME what elements you want to
convert to FME features; this is done by defining what we call a feature path, or the node path,
from the document root to the object we want to extract.
This feature path is stored in the elements to match parameter.
There is now a tree control that helps define feature paths automatically; this removes
a lot of the guesswork about how to read a new XML dataset.
Notice the tree hierarchy reflects the hierarchy in the XML file; the key is to select just
the node that you want to become a feature type.
You do not need to select its children unless you want them to become separate feature types.
Let’s select just the building element for now.
FME will add a new XML reader with the new building source feature type.
Click on the arrow beside the building feature type name to see its schema.
FME has read the XML from the perspective of the building element, so the attribute
list reflects the XML elements underneath it.
Let’s compare the XML file to our FME building feature type.
The building contains its own properties such as address and building ID and it also contains
child elements such as rooms and walls.
Any elements that contain their own children are named in the form of parent.child - if
there are multiple instances of a child element like room, they are in the form of list attributes.
Finally when child elements contain their own children they are in the form of embedded
list attributes.
If we wanted to work with these list attributes would use listed transformers to do so.
Let's create a new workspace and read this XML document again, this time we will read
the room element as a separate feature type along with the building element.
Drag the XML file onto the canvas again and open the XML reader parameters.
This time under elements to match select building and room.
Both feature types are added to the canvas.
Note that, for the room element, the XML is now read from the perspective of room and
below.
No building level elements are visible in the room attributes.
To link the two you would need to link the room.ID from the room feature with the associated
room{}.ID list element stored on the building feature.
Let’s add inspectors to each feature type so that we may inspect the data.
Note that there is only one building feature but three room features.
Also note that the building contains a list of rooms and a list of walls in its attribute
list whereas room only contains the wall list.
Congratulations you have read an XML file and generated FME features from it.
As you can see, there is more than one way to read the same XML source data with FME.
How you read it depends on what you need to get out of it and what perspective is the
most useful.