XML Reader Configuration

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

  • FME 2023.1

Introduction

In this tutorial, we will read and inspect some XML data. More specifically, we’ll examine how to use feature paths and flattening to read an XML building file. This simple XML describes a building feature. The building has address and location details, it contains rooms, and the rooms contain walls. We’ll use FME Workbench to demonstrate how to work with feature paths and flattening.

 

Configuring Feature Paths

There are three configuration types for the XML reader. XFMap or XRS files represent scripted configuration files for advanced users only, so let's focus on the 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. Feature paths have the form: Root/Parent/Object, where Object is the element we want to extract as features. You can also define a feature path simply by using the node name so long as it’s unique in the dataset and doesn't occur at a different position in the structure. So instead of Root/Parent/Object we could just use Object. This feature path is stored in the Elements to Match parameter.

Fortunately, there is a tree control that helps define feature paths automatically. This removes a lot of the guesswork about how to read a new XML dataset. 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.
 

Step-by-Step Instructions

1. Open FME Workbench
Open FME Workbench and create a new workspace. Then click on the Reader button to add a new reader. For Format, select XML (Extensible Markup Language), and then for Dataset browse to the safe_building_demo.xml that can be downloaded from the Files section of this article. Next, open the Parameters. 
Reader.png

As mentioned above, Feature paths allow us to query XML by defining the node in the XML structure from which we want to extract features. This is done with the Elements to Match parameter. For Elements to Match, click on the ellipsis to open the dialog. 
Parameters.png

FME can convert any XML element into an FME feature, so here we see every element in the XML file available to select. Click the drop-down arrow next to Dataset to expose the Building element. Then check the entire Building element. Click OK three times to add the reader to the canvas. 
 
3. View the Reader Schema
We’ve now added a new XML reader to the canvas with the Building source feature type. Click on the arrow beside the Building feature type name to see its schema.
ReaderSchema.png

FME has read the XML from the perspective of the Building element, so the attribute list reflects the XML elements underneath it. The Building contains its own properties, such as Address and 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, such as Location.Latitude. 
If there are multiple instances of a child element, like Room, they are in the form of list attributes, which is indicated by the { }. Finally, when child elements contain their own children, they are in the form of embedded list attributes. If we wanted to work with these, we’d use the List transformers to do so. For more information on working with lists see Tutorial: Getting Started with List Attributes
 
4. Read in a Separate Child Element
Let’s read this XML document again, this time reading the Room element. 
Add another Reader, this time in the Parameters, for Elements to Match uncheck Building. Then, expand the Building element and select Room. Click OK three times to add the Room feature type to the canvas. 
RoomElement.png

Note that we could also have selected both Building and Room at the same time to read them using the same XML reader - either way works. The result is the Room feature type shown here:
RoomSchema.png

Note that the XML is now read from the perspective of Room and below. No Building level elements are visible. 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.
 
5. View the Data in Visual Preview
Now, let’s see what the actual XML data looks like when viewed in Visual Preview or FME Data Inspector. 
For Visual Preview, click on the Building reader feature type to open the popup menu and then select View Source Data. The data will then be shown in Visual Preview. 
ViewSource.png


Once you have viewed the Building data, repeat with the Rooms dataset.


Note that there is only one Building feature but three Room features. Also, note that Building contains a list of Rooms and a list of walls whereas Room only contains the Wall list. Finally, note that Building has Room{0}.id = Admin_100, which corresponds to the Room.id = Admin_100 in Room.


In FME 2023.1, the Feature Information Window was revamped, and the XML elements are now shown in their hierarchy. 
FeatureInformation23.png

 
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. We could have just read everything from the dataset perspective, but then you would have a lot of nested lists to explode out using list transformers before you could do anything useful with the features.

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.