ActionScript 2.0 Code Sample
This is an .fla file containing ActionScript (2.0) that connects to the OpenAmplify open API and parses the XML.
File has been updated for use with OpenAmplify Version 2.0
Overview:
The easiest way to connect to the OpenAmplify API from ActionScript 2.0 is with a POST request using the sendAndLoad() method of the LoadVars object.
Setting Parameters:
The parameters to to the API should be set as properties of the LoadVars object. For this example, we'll create a LoadVars object called myLV, and set properties for it that correspond to the names of the parameters the API expects.
var myLV:LoadVars = new LoadVars(); // INSERT YOUR OWN API KEY HERE! myLV.apiKey = "12345"; // OPTIONS: all (default), actions, topics, demographics, styles myLV.analysis = "all"; // OPTIONS: xml (default), json, dart myLV.outputformat = "xml"; myLV.inputText = ""; // USE EITHER inputText OR sourceURL! //myLV.sourceURL = "";
Note: Not all the parameters need to be present in the LoadVars object. The only necessary parameters are apiKey and either textInput or sourceUrl.
Note: The API is case-insensitive, but ActionScript is not. Therefore, you may give myLV a property of InPuTtExT if you like, but you must call it the same way wherever you reference it within ActionScript.
Where to put the data: XML object
To receive the XML from the API, we create an XML object called myXML and set ignoreWhitespace to true for convenience. We set its onLoad function to parseMyXML, which we will expand later to parse the received XML.
var myXML:XML = new XML();
function parseMyXML(success) {
if(!success) {
trace('failed to get XML');
}
else {
trace('Yay! XML!');
}
}
myXML.ignoreWhitespace = true;
myXML.onLoad = parseMyXML;
Issue Request:
To send the parameters set in myLV and receive XML back from the API, we use the LoadVars.sendAndReceive() method.
In this example, we assume there is a button or movieclip on the stage that calls the function, submitToAPI() on the onPress event. This function sets inputtext equal to the contents of a text area on the stage with the instance name myTA.
function submitToAPI() {
// GET TEXT FROM myTA input
myLV.inputText = myTA.text;
// USE sendAndLoad. LOAD XML TO myXML.
myLV.sendAndLoad("http://portaltnx20.openamplify.com/AmplifyWeb_v20/AmplifyThis", myXML, "POST");
}
Parse XML:
While ActionScript 3.0 provides very robust XML navigation, if you're using AS 2.0 you probably want to parse the XML and put the data into more easily accessible arrays and/or objects.
The following parseMyXML() function is expanded to parse through all parts of the XML returned when analysis is set to all.
function parseMyXML(success:Boolean) {
if(success) {
//trace(result_XML.toString());
var navigator:Array = this.firstChild.firstChild.childNodes;
for(var i:Number=0;i<navigator.length;i++) {
if(navigator[i].nodeName == 'Topics') {
parseTopics(navigator[i].childNodes);
}
else if(navigator[i].nodeName == 'Actions') {
parseActions(navigator[i].firstChild.childNodes);
}
else if(navigator[i].nodeName == 'Demographics') {
parseDemographics(navigator[i].childNodes);
}
else if(navigator[i].nodeName == 'Styles') {
parseStyles(navigator[i].childNodes);
}
}
// place stuff in right text area;
displayResults();
}
else {
trace('no luck');
}
}
// Set up variables for holding the output;
textInfo = new Object;
textInfo.topics = new Array();
function parseTopics(topics) {
// FIND TopTopics
for(var i:Number=0;i<topics.length;i++) {
if(topics[i].nodeName == "TopTopics") {
var nav = topics[i].childNodes;
for(var n:Number=0;n<nav.length;n++) {
var holderObj = new Object();
tempTopic = nav[n].childNodes;
for(var j:Number=0;j<tempTopic.length;j++) {
switch (tempTopic[j].nodeName) {
case "Topic":
holderObj.name = tempTopic[j].childNodes[0].firstChild.nodeValue;
holderObj.prominence = tempTopic[j].childNodes[1].firstChild.nodeValue;
break;
case "Polarity":
holderObj.minPolarity = tempTopic[j].childNodes[0].childNodes[1].firstChild.nodeValue;
holderObj.meanPolarity = tempTopic[j].childNodes[1].childNodes[1].firstChild.nodeValue;
holderObj.meanPolarityName = tempTopic[j].childNodes[1].childNodes[0].firstChild.nodeValue;
holderObj.maxPolarity = tempTopic[j].childNodes[2].childNodes[1].firstChild.nodeValue ;
break;
case "OfferingGuidance":
holderObj.offeringGuidance = tempTopic[j].childNodes[0].firstChild.nodeValue;
holderObj.offeringGuidanceScore = tempTopic[j].childNodes[1].firstChild.nodeValue;
break;
case "RequestingGuidance":
holderObj.requestingGuidance = tempTopic[j].childNodes[0].firstChild.nodeValue;
holderObj.requestingGuidanceScore = tempTopic[j].childNodes[1].firstChild.nodeValue;
break;
}
}
textInfo.topics.push(holderObj);
}
}
// place DOMAINS and/or LOCATIONS here if needed later
}
}Bugs and Features:
"Transferring data from ..."
In Firefox, there's a known bug affecting the browser's status bar when requests are made with the sendAndReceive method. The status bar shows "Transferring data from ..." even after the data have finished loading. This appears to be only a cosmetic problem and doesn't affect the browser's or the Flash player's memory or performance.
