How to read an XML loaded from a URL? - actionscript-3

I am trying to load the Forex Feed from an XML stored at http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml. I followed the answer from this question. Here is my code:
var xml:XML;
var list:XMLList;
var xmlLoader:URLLoader = new URLLoader(new URLRequest("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"));
xmlLoader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
function onComplete(e:Event):void
{
xml = new XML(e.target.data);
list = xml.Cube.Cube;
trace(xml);
trace(list);
}
The xml gets traced, but not the list. I followed the answer from that other question. I still don't see the list in the ouput window. Any help is greatly appreciated.

Related

Flash as3 - API request with custom headers: POST but appears as GET (IOError #2032) [duplicate]

This question already has answers here:
URLRequest/URLLoader auto-converting POST request to GET
(2 answers)
Closed 7 years ago.
i have this file that makes an API request somewhere to retrieve JSON data.
The request needs to have 2 custom headers. I've read that custom headers require the request to be a POST.
I've read all the previous questions here and also on other sites, i think the code shows it pretty well... The "funny" part is that at some point i created a logger (environment: Tomcat) to see why it wasn't loading, and apparently the request i'm sending is a GET even if i've specified for it to be a POST...
Code
var url:String = "your_url_here";
var headers:Array = [
new URLRequestHeader("user-id","your_user-id"),
new URLRequestHeader("custom-auth","your_custom_auth_code"),
new URLRequestHeader("Accept","application/json")
];
var request:URLRequest = new URLRequest();
request.requestHeaders = headers;
request.method = URLRequestMethod.POST;
request.contentType = "application/json";
request.url = url;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, decodeJSON);
loader.addEventListener(IOErrorEvent.IO_ERROR, handleError);
loader.load(request);
private function handleError(e:IOErrorEvent):void{
sometextcontainer.txt01.text = e.toString();
}
private function decodeJSON(e:Event) {
var Info:Object = JSON.parse(e.target.data);
// text fillers
}
The error: #2032 STREAM ERROR but i was 100% sure about the URL being correct, so i made a logger to actually see what was happening on the other side and surprise surprise... The request appears to be a GET!
What did i do wrong? Ideas?
Note1: i don't have sandbox security problems as i already placed Global exceptions;
Note2: i tried the request in REST with the custom headers (as POST) and it works (and the logger says i actually made a POST...)
Thanks for your kind help! :)
That made the trick.
I'm adding the code in order to make everyone understand it better and quicker. Basically, i added the following:
var datareq:URLVariables = new URLVariables();
datareq.dummyTest = "true";
request.data = datareq;`
So now it looks like:
var request:URLRequest = new URLRequest();
var datareq:URLVariables = new URLVariables();
datareq.dummyTest = "true";
request.data = datareq;
request.requestHeaders = headers;
request.method = URLRequestMethod.POST;
request.contentType = "application/json";
request.url = url;
Thanks for the help.

Update xml file node attribute in actionscript

We need to modify/update the xml attribute of .idms file. using actionscript. I'm using below approach to do that.
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
var file:File = File.desktopDirectory.resolvePath('myInputFile.xml');
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);// doesn't read processing instrunction
var xml:XML = new XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
fileStream.close();
var writeStream:FileStream = new FileStream();
writeStream.open(file, FileMode.WRITE);
for each(var node:XML in xml.descendants("*"))
{
if(node.localName() == "Link")
{
var linkfile:String=node.#LinkResourceURI = "file:/Shared/Logos/Bk.ai";
}
}
writeStream.writeUTFBytes(String(xml));
writeStream.close();
}
In above code problem is that it does not read processing instruction on top of xml file. in result after writing the xml file, it won't read the file as InDesign snippet file anymore.
Processing instrunctions are:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Can anyone help me in figure out how I can I read the processing instrunction as well in new file ?
Thanks for quick reply but Even though I've made this property as flase it's keep ignoring the processing instuction.
var file:File = File.desktopDirectory.resolvePath('031413115849909.idms');
var fileStream:FileStream = new FileStream();
XML.ignoreProcessingInstructions = false; //newly added
fileStream.open(file, FileMode.READ);
var xml:XML = new XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
fileStream.close();
There is a static property in the XML class called ignoreProcessingInstructions (which is true by default) that you could try setting before reading in your file. See this help page.
After some Google-ing, it appears that processing instructions outside of the root node may not be available, since the XML object starts with the root node. In that case, you may have to read the file in as standard text, save the processing line(s) before the root node, and then write them to the output file before you write the actual XML contents.

AS3 post xml ULRequest

I am having trouble posting an xml file with the URLLoader.
After it loads it seems that there is no XML attached to the request, could any
please clarify what i am doing wrong here (banging head against the wall)
// code
private function postXmlFiles():void
{
var a:XML = new XML(new EmbeddedXML());
var r:URLRequest = new URLRequest("http://vms.api.apic.co/rest/v3/addmedia");
r.data = a;
r.method = URLRequestMethod.POST;
r.contentType = "text/xml";
l = new URLLoader();
l.dataFormat = URLLoaderDataFormat.TEXT;
// Handlers
l.addEventListener(Event.COMPLETE, on_complete);
l.load(r);
}
private function on_complete(e : Event):void
{
trace('loaded!:' + l.data); // this is returning "loaded!: No xml file attached to the request."
}
I tested your code, replacing one string:
var a:XML = new XML("<test/>");
Trace output changed, indicating l.data field actually contains something.
I also used debugging proxy, and examined request body, it does contained my <test/> string.
You should test your new EmbeddedXML() call to see what it returns.

How to use a variable as part of an URL

I have a variable
var qstAccessCode:String = "default";
and a loader with URLRequest
var qst:XML;
var qstLoader:URLLoader = new URLLoader();
qstLoader.load(new URLRequest("http://dl.dropbox.com/u/44181313/Qaaps/Audio/" + qstAccessCode + ".qst"));
qstLoader.addEventListener(Event.COMPLETE, processQST);
function processQST(e:Event):void {
qst = new XML(e.target.data);
trace("QST loading");
}
I would like to use the value of qstAccessCode to complete the URL (so I can change the URL based on user input - if no input then use "default") but I get an error:
"1120: Access of undefined property qstAccessCode"
Is this to do with scoping? How can I complete the URL? Thanks in advance.
Edit: I haven't been able to get clear on this, so I'm also going to look at generating the complete URL from the user-input function and see if I get the URLRequest to pick it up as a variable. If there are any further comments on the original idea I will be very grateful to read them. Cheers.
Edit: #Moorthy I have qstAccessCode defined like this:
var qatAccessCode:String = "default";
var stageText:StageText = new StageText();
stageText.returnKeyLabel = ReturnKeyLabel.GO;
stageText.stage = this.stage;
stageText.viewPort = new Rectangle(225, 765, 200, 35 );
stageText.addEventListener(Event.CHANGE, onChange);
function onChange(e:Event):void
{
qatAccessCode = stageText.text;
trace(qatAccessCode);
}
It traces keyboard entry when I test movie (Air 3.2 for Android).
qstAccessCode should be defined in the same scope as the URLRequest.
You must defined property qstAccessCode like:
var qstAccessCode:string;
qstAccessCode's value is your url address.

How to read .xlsx file contents in flex3

I need to know how to read .xlsx file contents in flex3. Does anyone comes across this problem please give some idea to accomplish this task. If any good API please let me know.
I tried to read .xlsx file using the below sample code but its not working, but its working fine for .xls file.
// on creation complete
private function onCreate(e:Event):void
{
var request:URLRequest = new URLRequest("C://test.xlsx");
var urlLoader:URLLoader = new URLLoader(request);
urlLoader.addEventListener(Event.COMPLETE, onURLLoaderComplete);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(request);
}
// on file load complete
private function onURLLoaderComplete(event:Event):void
{
loadedFile = event.target.data;
var excelFile:ExcelFile = new ExcelFile();
excelFile.loadFromByteArray(loadedFile);
sheet = excelFile.sheets[0];
Alert.show(sheet.getCell(1,0).value)
DataGrid.dataProvider=sheet.values;
}
Thanks in Advance.
If you only need cell values, save it as CSV - it is very simple to parse (unlike xlsx.)