Solved

How to extract XML attributes into seperate FME features attributes

  • 30 January 2018
  • 9 replies
  • 204 views

Badge +3

My xml code (contained in an attribute of my incoming features) looks like this:

<?xml version="1.0" encoding="UTF-16"?><document authority="Forstamt Kanton Thurgau" authority_url="" category="main" cycle="Öffentliche Auflage" doctype="decree" enactment_date="2007-10-31" federal_level="Kanton" id="3039" instance="Gemeinderat" number="" subtype="Waeldi" title="AWB Detailplan Nr. 1" type="Statische Waldgrenze"><file category="main" href="/api/attachments/9989" title="AWB Entscheid-Ersatz.pdf"></file></document>

How am I to extract the XML attributes (e.g. 'authority', 'authority_url', 'category', 'cycle', etc.) into seperate FME attributes of my features.

In other words: I want to extract 'Forstamt Kanton Thurgau' into an
attribute called 'authority', 'Öffentliche Auflage' into an
attribute called 'cycle', and so on.

icon

Best answer by david_r 30 January 2018, 11:20

View original

9 replies

Userlevel 4

I would simply extract all the attributes into a single feature, then send the feature into two different directions (effectively duplicating the feature). If you want, you could insert two different AttributeKeepers to trim down the number of attributes on each feature copy.

Badge +3

I would simply extract all the attributes into a single feature, then send the feature into two different directions (effectively duplicating the feature). If you want, you could insert two different AttributeKeepers to trim down the number of attributes on each feature copy.

I am afraid I do not understand your answer.

 

I have features coming from the reader with an attribute containing the XML code (such as above).

 

Now I want to fill e.g. 'Forstamt Kanton Thurgau' into an feature attribute called 'authority', 'Öffentliche Auflage' into an feature attribute called 'cycle', and so on.

 

How am I to do this in an FME Workspace?

 

 

Userlevel 2
Badge +17

Hi @frantsch, you can use the XMLFlattener to flatten the XML attributes into FME feature attributes.

Set the attribute containing the XML document to the XML Attribute parameter, and set "document" to the Elements to Match parameter.

Just be aware that the transformer won't expose resulting attributes automatically. If necessary, you will have to expose their names (e.g. 'document.authority', 'document.category', 'file.category', 'file.href', etc.) manually through the Attributes to Expose parameter. Have a look at the flattened result on the Feature Information Window in FME Data Inspector to check how the transformer flattens the XML elements/attributes.

In addition, the 'UTF-16' encoding declaration could cause an XML parsing error depending on your system locale. If such an error occurred, consider removing the XML declaration (<?...?>) with a StringReplacer, or encoding the document to 'UTF-16' explicitly with the AttributeEncoder.

Userlevel 4

Have a look at the attached sample workspace. Hopefully that makes it clearer.

xml-authority.fmw

Badge +3

Hi @frantsch, you can use the XMLFlattener to flatten the XML attributes into FME feature attributes.

Set the attribute containing the XML document to the XML Attribute parameter, and set "document" to the Elements to Match parameter.

Just be aware that the transformer won't expose resulting attributes automatically. If necessary, you will have to expose their names (e.g. 'document.authority', 'document.category', 'file.category', 'file.href', etc.) manually through the Attributes to Expose parameter. Have a look at the flattened result on the Feature Information Window in FME Data Inspector to check how the transformer flattens the XML elements/attributes.

In addition, the 'UTF-16' encoding declaration could cause an XML parsing error depending on your system locale. If such an error occurred, consider removing the XML declaration (<?...?>) with a StringReplacer, or encoding the document to 'UTF-16' explicitly with the AttributeEncoder.

Takashi, you are - once again - my hero! Your hint on how to expose the XML attributes explicitely through the 'Attrbutes to Expose' parameter did the Job!

 

I had tried that before, however did not know I had to put the 'document.' in front of the XML attribute name. The transformer help is not clear on that. Have many thanks!

 

 

Badge +3

Have a look at the attached sample workspace. Hopefully that makes it clearer.

xml-authority.fmw

Which Version of FME Desktop is this Workspace for? Could not open it with FME Desktop 2017, nor 2015...

 

 

Userlevel 4
Which Version of FME Desktop is this Workspace for? Could not open it with FME Desktop 2017, nor 2015...

 

 

The workspace was made with FME 2017.1.

 

You can download it here: https://www.safe.com/support/support-resources/fme-downloads/
Badge +3
The workspace was made with FME 2017.1.

 

You can download it here: https://www.safe.com/support/support-resources/fme-downloads/
Ok, I was able to open it with 2017.1, thank you. Now I saw that you have the exposal of the attributes implemented there as proposed by takashi, as well. That solved the problem.

 

 

Userlevel 4
Ok, I was able to open it with 2017.1, thank you. Now I saw that you have the exposal of the attributes implemented there as proposed by takashi, as well. That solved the problem.

 

 

Excellent, am glad to hear.

Reply