Skip to main content

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.

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

 

 


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.


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

xml-authority.fmw


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!

 

 


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

 

 


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

 

 


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