Skip to main content
Solved

How to extract XML attributes into seperate FME features attributes

  • January 30, 2018
  • 9 replies
  • 725 views

frantsch
Contributor
Forum|alt.badge.img+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.

Best answer by david_r

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

xml-authority.fmw

View original
Did this help you find an answer to your question?

9 replies

david_r
Evangelist
  • 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+3
  • Author
  • Contributor
  • January 30, 2018
david_r wrote:

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
Contributor
Forum|alt.badge.img+19
  • Contributor
  • 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
Evangelist
  • 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+3
  • Author
  • Contributor
  • January 30, 2018
takashi wrote:

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+3
  • Author
  • Contributor
  • January 30, 2018
david_r wrote:

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
Evangelist
  • January 30, 2018
frantsch wrote:
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+3
  • Author
  • Contributor
  • January 30, 2018
david_r wrote:
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
Evangelist
  • January 30, 2018
frantsch wrote:
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.

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings