views:

33

answers:

1

hi, We are collecting the data at run time in Collection object in JSP page. We want to read this data from the JSP in a MXML through actionscript. pls share the sample if you have.

thanks, Sudarshan

A: 
function loadData():void
{
  var ldr:URLLoader = new URLLoader();
  var request:URLRequest = new URLRequest("page.jsp");
  ldr.addEventListener(Event.COMPLETE, onLoad);
  ldr.load(request);
}
private function onLoad(e:Event):void
{
  var ldr:URLLoader = URLLoader(e.target);
  trace(ldr.data);//traces the loaded string
  //if the data is xml
  /*
  var myxml:XML = new XML(ldr.data);
  trace(myxml.toXMLString());
  */
  //update: answer to the comment:
  //If the input string just lacks a root tag from being valid xml, 
  //you can introduce a dummy root tag.
  var myxml:XML = new XML("<root>" + ldr.data + "</root>");
  trace(myxml.data.toString());  //Hello
  trace(myxml.value.toString()); //Hi
}

page.jsp should serialize the collection to appropriate format (xml/json/whatever) and return it.

Amarghosh
Thanks a lot. But i have one more problem here.In my JSP i have XML tag but i am unable to get value .for example.my jsp is like.<data>Hello</data><value>Hi</value>So if i want to get the value of data or value how can i get.from above example what you gave i am getting following error.TypeError: Error #1090: XML parser failure: element is malformed.pls help..
@user the string passed from jsp should be a valid xml - your string isn't valid xml - there's no root tag. Add the root tags at either jsp or flash end.
Amarghosh
@Amarghosh .. I have already added the root tag. if you can give one example may be that may help. But i have one more doubt if i write file name as .jsp but asking for XML wont it crate the above error?
@user What do you get for `trace(ldr.data);`? No, the jsp file should create an xml string and return it - post your jsp code
Amarghosh
i am getting full file content. My JSP is:<?xml version="1.0" encoding="utf-8"?><Start><test><data>A</data></test><test><data>B</data></test></Start>
That's not your jsp, that's just plain xml; I thought you wanted to read a serialized Collection from jsp. For this one you can just do `var myxml:XML = new XML(ldr.data); trace(myxml.test.data[0], myxml.test.data[1]);` - and you can use `.xml` extension for the file
Amarghosh
Still its in development stage so i havent wrote full code. I need to get the value from servlet at run time and put this value in the script.