Working with XML in Twyla


Our other tutorials have introduced you to the concept of creating Choreos in Tywla and dealing with JSON. In this tutorial we'll show you how to work with XML responses. The good news is that working with XML in Choreos is a breeze. The Temboo platform has an excellent XPATH library that makes the syntax for dealing with XML very simple.

Let's get started

1Starting with your original HTTP Request Choreo from our first tutorial, modify the URL field on the Server tab in the HTTP Request Step so that you're using the Google Directions XML endpoint instead of the JSON one. Here's the endpoint you need:

http://maps.googleapis.com/maps/api/directions/xml

2Next, drag an Iterate Step to the canvas and connect the HTTP Request step to it via the HTTP Request Step's "Next" arrow. Now, create two new String type variables called CurrentName and CurrentValue. In the Iterate Step property editor, specify the Response variable as the Collection to iterate over, and specify CurrentName and CurrentValue in the corresponding fields, as shown in the screenshot below. You can use the [...] icon to enable an XPATH expression field for the Response collection. In this case we'll want to specify the path to the html_instructions node:

/DirectionsResponse/route/leg/step/html_instructions

Setting up the Iterate step to work with XML data

3Next, we'll append the CurrentValue to a variable called Instructions. Drag a new Expression Step to the canvas and then create a new variable called Instructions defined as a "String". Edit the Expression Step so that it looks like the one in the image below, and then draw a connection from that back to the Iterate Step. Here's the code you need for the assignment expression:

<<{Instructions} "\n" {CurrentValue}>>

Appending the value of CurrentValue to the Instructions variable

4That's it. You can test out the Choreo by switching to Test mode, entering some values for the inputs and hitting the Start button. The XML response will be parsed, leaving your Instructions variable containing details on how to get from your origin to your destination.

Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.

We're hiring!

Like what we do? Take a look at our open positions.


Back