Skip to main content
Solved

How to extract XML attributes into seperate FME features attributes

  • January 30, 2018
  • 9 replies
  • 947 views

frantsch
Contributor
Forum|alt.badge.img+4

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.

Best answer by david_r

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

xml-authority.fmw

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

9 replies

david_r
Celebrity
  • 8394 replies
  • January 30, 2018

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.


frantsch
Contributor
Forum|alt.badge.img+4
  • Author
  • Contributor
  • 27 replies
  • January 30, 2018

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?

 

 


takashi
Celebrity
  • 7843 replies
  • January 30, 2018

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.


david_r
Celebrity
  • 8394 replies
  • Best Answer
  • January 30, 2018

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

xml-authority.fmw


frantsch
Contributor
Forum|alt.badge.img+4
  • Author
  • Contributor
  • 27 replies
  • January 30, 2018

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!

 

 


frantsch
Contributor
Forum|alt.badge.img+4
  • Author
  • Contributor
  • 27 replies
  • January 30, 2018

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...

 

 


david_r
Celebrity
  • 8394 replies
  • January 30, 2018
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/

frantsch
Contributor
Forum|alt.badge.img+4
  • Author
  • Contributor
  • 27 replies
  • January 30, 2018
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.

 

 


david_r
Celebrity
  • 8394 replies
  • January 30, 2018
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.