Since a lot of API responses are in XML, we'll review XML parsing in Node.js to help you get to the interesting data faster.
This tutorial assumes that you've already gone through our Node.js getting started tutorial and are familiar with how our Node.js SDK works.
Node does not incldue any XML parsing capabilities in its standard library. This example uses Node ElementTree, a Node implementation of a popular Python XML library. It can be installed using the Node Package Manager, by running the following command in the directory containing the node_modules directory with the unzipped Temboo SDK.
npm install elementtree
1 Log in to Temboo and go to the Yahoo > Weather > GetWeatherByAddress Choreo in our Library.
2 Enter a location in the Address input and click Generate Code to test the Choreo from our website.
3 You get a whole bunch of XML in the Response output with lots of weather-related information about the location. Next we'll see how to parse through this response in Node.js and pick out only the pieces we're interested in.
4 Create a new Node.js file and copy in the code below. Steps 5 & 6 talk you through what happens in the code.
var et = require("elementtree"); var tsession = require("temboo/core/temboosession"); var Yahoo = require("temboo/Library/Yahoo/Weather"); var session = new tsession.TembooSession("ACCOUNT_NAME", "APP_NAME", "APP_KEY"); var getWeatherByAddressChoreo = new Yahoo.GetWeatherByAddress(session); // Instantiate and populate the input set for the choreo var getWeatherByAddressInputs = getWeatherByAddressChoreo.newInputSet(); // Set inputs getWeatherByAddressInputs.set_Address("104 Franklin St, New York, NY"); // Run the choreo; on success parse XML response and report key info. getWeatherByAddressChoreo.execute( getWeatherByAddressInputs, function(results) { var xmlData = et.parse(results.get_Response()); var condition = xmlData.find("channel/item/yweather:condition"); var temp = condition.attrib.temp; var text = condition.attrib.text; console.log("The weather is %s with a temperature of %s.", text, temp); }, // On failure, log errors returned by Temboo. function(error) { console.log(error.type); console.log(error.message); } );
5 First we have to convert the XML response from Yahoo to a w3c.Document object.
6 Next we need to parse out the data we want from the XML file. It helps to look at the XML file's structure to get an idea of how it is organized. We want the text and temp attributes. You can find them in:
<channel><yweather:condition>
.
7That's it. You can run the code in your Node.js IDE to try it out. You should see the weather condition text and temp printed in the console.
Now you're ready to tackle all sorts of XML parsing tasks in Node.js. You can find lots of XML to play with in our Library of 2000+ Choreos.
Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.
We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.
Like what we do? Take a look at our open positions.