span8
span4
span8
span4
Completed workspace as a template
XML is a form of structured text that is commonly used in open standards and exchange formats.It is also frequently encountered in web and messaging applications.Being able to convert data to XML is an important capability of FME.This article covers FME's approach to writing XML and includes a basic example to help you get started.It assumes some understanding of basic FME and XML concepts.For example,the term ‘element' in XML can relate to either a feature or an attribute in FME terms,depending on how the XML document is interpreted.SeeFME Approach to XMLfor more background.
FME uses a template approach to writing XML.The XML document structure is placed in or referenced by an XMLTemplater transformer.Then fme:get-attribute functions are placed within the template at the locations where you want feature attribute values to be merged into the document.This functions much like a mail merge operation,where a letter template receives name and address values from each record to dynamically generate customized messages.
XML documents can be generated one per feature such as for dataset metadata.Or root and sub-templates can be used to model a document that has one root with multiple child elements based on a multi-record dataset.The XML document structure can be generated from an XSD using the XMLSampleGenerator or obtained from a sample XML output document.Then element values are replaced with FME field values using fme:get-attribute functions where we want the content to be driven by the source data.The XML that is produced by XMLTemplater is stored in a single _result attribute that can then be validated and formatted with the XMLValidator and XMLFormatter transformers.In this exercise we use sample XML to seed the template,and will leave validation and formatting to later exercises.
Typically the final output is written to disk using the Text File writer since the XML result attribute is just a large text field.The XML writer is not often used since it does not yet support reading schema from XSD.There are special cases where the XML writer is beneficial,such as using fragment mode to write very large XML datasets.This advanced approach allows XML fragments to be written as they are received rather than building a buffer of the entire document before writing as is the case with the Text File writer.
The following exercise shows how to take a single record fed into an XMLTemplater template,and generate a basic XML document that matches the structure found in 亚搏在线Safe_Building_Demo.xml.
1.Open FME Workbench.
Start with a blank workspace.Drag and drop the attached source CSV file: Building.scv.
2.Add an XMLTemplater.
The first step for converting this to XML is to generate the root element.To create the XML template we copy the contents from the root object of an example of the type of XML file we would like to generate - in this case the root from 亚搏在线Safe_Building_Demo.xml.
Add an XMLTemplater,connect the CSV source building feature type to it,edit the XMLTemplater and place the following within the ROOT template:
street address city BC Canada 0 0
3.Connect a Logger after the XMLTemplater and run the workspace.
Note that the template is simply written to the output within the _result field.
What we really want is to merge the values from the input CSV record into the template before we write the XML.To do this we just open up the XMLTemplater,edit ROOT,click on the contents of each element we want to merge values into and then click on the attribute we want to merge values from.This should insert the attribute function associated with the attribute we selected ({fme:get-attribute("Building_id")} etc) into the location of the template where we positioned the cursor.
Once you have done this for all the available attributes you should have a root template that looks like this:
{fme:get-attribute("Address")} {fme:get-attribute("City")} {fme:get-attribute("Province")} {fme:get-attribute("Country")} {fme:get-attribute("Longitude")} {fme:get-attribute("Latitude")}
4.Finally,to write this to disk,from the Writers menu,add a Text File writer.
Set the destination file path to building_output.xml and then set the output MIME type to text/xml.This can be important for displaying the xml correctly in some viewers such as web browsers.In order to feed the text file writer set the XMLTemplater ‘Result attribute' to be "text_line_data".
5.Run the workspace and examine the resulting building_output.xml output.See how these values are merged into the XML document structure.You should see the following output.Note how the fme:get-attribute("") functions have been replaced by the feature attribute values:
7445 132 St. Surrey BC Canada -122.860 49.138
The following exercise shows how to take the workspace from 1.a and add multiple child elements based on input records using an XMLTemplater subtemplate.
Going back to our original task,we want to use FME to generate XML content that matches the structure found in 亚搏在线Safe_Building_Demo.xml.We generated the root content in example 1a,so the next step is to generate the Room elements.
1.First,add a new CSV reader to read Rooms.csv.
2.Next,edit the XMLTemplater.
Check Sub Templates and click on ‘+' to add a new subtemplate.
Figure 1.XMLTemplater configuration with subtemplate
As before we start the template with a single example instance or element of the object we want to generate.In this case,we need an example of the Room element,which we can get from the first room in 亚搏在线Safe_Building_Demo.xml:
Reception Admin 12
Then click OK to save the change to the XMLTemplater.You will notice a new input port on the XMLTemplater called ‘SUB'.We need to connect the Room input features to this in order to have the fields we need available to the XMLTemplater on the feature schema.
Now that the room feature type is connected,we can replace the values with the values we read from the input CSV file.This should yield a sub-template that looks like this:
{fme:get-attribute("Name")} {fme:get-attribute("Category")} {fme:get-attribute("Area")}
To complete the XMLTemplater configuration,we need to tell the root template to call the subtemplate.This is done using the fme:process-features("SUB") function.Add this to your root template so that the Room child elements are inserted at the end of the Building parent object.
{fme:get-attribute("Address")} {fme:get-attribute("City")} {fme:get-attribute("Province")} {fme:get-attribute("Country")} {fme:process-features("SUB")} {fme:get-attribute("Longitude")} {fme:get-attribute("Latitude")}
3.Add an XMLFormatter after the XMLTemplater.
Finally,its a good idea to format our xml before writing to disk.This makes it a lot easier to review our output and troubleshoot any problems,and has the added bonus of catching syntax errors since only valid XML can be formatted.We will look at validation in more detail later.
Add an XMLFormatter after the XMLTemplater.Set the ‘Attribute with XML text' and the ‘Attribute to contain XML output' to: "text_line_data".Leave the rest of the settings with their default values (Formatting type should be "Pretty-print XML".
4.Connect the output of the XMLFormatter to the text_line output feature type.
Run the workspace.Your output should appear as below.Note the multiple Room elements that are inserted inside the Building object after the Location element:
7445 132 St. Surrey BC Canada -122.860 49.138 Reception Admin 12Sales Office Sales 20Meeting Room Meetings 25
The completed workspace is shown below after a run.Note the XMLTemplater has one input feature for the root Building element,3 input features for the Room elements and one output feature for the combined XML document's output.
© 2019 亚搏在线Safe Software Inc |Legal