Jump to content
GreenSock

alflasy

Access CDATA

Recommended Posts

How to access CDATA in loaderMax.

 

Something like

function completeHandler(event:LoaderEvent):void {

trace("load complete. XML content: "+loader.content.children().children();

}

 

I am not trying to load and images or swf just xml data that I want to display in rows and columns with some html attributes like color change and bold text.

 

Thanks

Link to comment
Share on other sites

Hey ya mate,

 

Assuming this for test XML data:

 

<data>

<testdata>

<testcdata><![CDATA["Here's some CDATA"]]></testcdata>

</testdata>

</data>

 

You can do the following:

 

var xl:XMLLoader = new XMLLoader("test.xml", new XMLLoaderVars()
.onComplete(xmlLoaded)
);
xl.load();


private function xmlLoaded(e:LoaderEvent):void 
{
trace(e.target.content.testdata.testcdata);
//Will echo "Here's some CDATA"
}

  • Like 2
Link to comment
Share on other sites

Thanks, but it gives error. Here my scenario

 

XML

 

<slides>

<ImageLoader name="img_0" url="images/img_0.jpg" estimatedBytes="60000" load="true" x="0" y="0" ><![CDATA[<b>Image 1</b> Description 1]]></ImageLoader>

<ImageLoader name="img_1" url="images/img_1.jpg" estimatedBytes="94000" load="true" x="320" y="0"><![CDATA[<b>Image 2</b> Description 2]]></ImageLoader>

<ImageLoader name="img_2" url="images/img_2.jpg" estimatedBytes="94000" load="true" x="0" y="200"><![CDATA[<b>Image 3</b> Description 3]]></ImageLoader>

<ImageLoader name="img_3" url="images/img_3.jpg" estimatedBytes="60000" load="true" x="320" y="200" ><![CDATA[<b>Image 4</b> Description 4]]></ImageLoader>

</slides>

 

and i am tracing it in onChildComplete.

I get to the name by

event.target.name

BUt can get to cdata

Link to comment
Share on other sites

Hi,

 

Zync, gave a great answer but he wasn't aware at the time that you were wrapping your cdata in an ImageLoader node.

 

You can format the xml the way you have it, but I opted to wrap my cdata in a <description> node as it is a bit cleaner.

 

Here is the xml

 

 

<slides>
<ImageLoader name="test" url="someImage.jpg" load="false">
<description><![CDATA[<b>Image 1</b> Description 1]]></description>
</ImageLoader>
</slides>

 

Once the XMLLoader loads that xml you have 2 ways of grabbing the cdata inside the description node

 

1: parse the xml object that was loaded. The loaded xml is accessible via the content property of the XMLLoader

 

2: When XMLLoader encounters an <ImageLoader> node that has additional xml markup inside of it, that additional xml is assigned to the ImageLoader's vars' rawXML. You can then target the ImageLoader with LoaderMax.getLoader() and then grab the rawXML

Read more about this here: http://www.greensock.../XMLLoader.html (3rd code example)

 

Both methods of accessing the cdata inside the ImageLoader's description node are shown below:

 

 

import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
LoaderMax.activate([imageLoader]);

var xml:XMLLoader = new XMLLoader("cdata.xml", {onComplete:xmlComplete});

xml.load();

function xmlComplete(e:LoaderEvent):void{
trace("xml = " + e.target.content);

//parse the xml directly and find the description node of the ImageLoader
trace("\ne.target.content.ImageLoader.description: \n" +e.target.content.ImageLoader.description);

//Access the rawXML property of the ImageLoader and search for its description node
trace('\nLoaderMax.getLoader("test").vars.rawXML.description: \n' + LoaderMax.getLoader("test").vars.rawXML.description);
}

 

This is the output of the traces:

 

 

xml = <slides>

<ImageLoader name="test" url="someImage.jpg" load="false">

<description><![CDATA[<b>Image 1</b> Description 1]]></description>

</ImageLoader>

</slides>

 

e.target.content.ImageLoader.description:

<b>Image 1</b> Description 1

 

LoaderMax.getLoader("test").vars.rawXML.description:

<b>Image 1</b> Description 1

I have attached a Flash CS5 fla with xml

cdata_cs5.zip

Link to comment
Share on other sites

Thank you Carl/Zync,

I have to little modify your code as I was trying to get the description in child complete listener.

 

function imageLoadComplete(event:LoaderEvent):void {
trace(event.target.vars.rawXML.description)
}

Link to comment
Share on other sites

So, this didn't ended here. Now what if I want to get the attributes value in description node. All my below attemped failed

 

event.target.vars.rawXML.description.vars.txtX

event.target.vars.rawXML.description.txtX

event.target.vars.rawXML.description.rawXML.vars.txtX

 

Thank you

Link to comment
Share on other sites

assuming your xml looks like this:

 

 

<slides>
<ImageLoader name="test" url="someImage.jpg" load="false">
<description txtX="200"><![CDATA[<b>Image 1</b> Description 1]]></description>
</ImageLoader>
</slides>

 

you would get the txtX attribute of the description node with:

 

trace("txtX = " + LoaderMax.getLoader("test").vars.rawXML.description.@txtX)
//output 200

 

here's some good resources for parsing xml:

http://www.republicofcode.com/tutorials/flash/as3xml/

http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4

 

search both pages for "attribute"

Link to comment
Share on other sites

That's my bad. I should have tried the basic s first.

 

Thank Carl.

Link to comment
Share on other sites

No problem. Glad to help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×