PDA

Click to See Complete Forum and Search --> : getElementByClass()



ericflash
01-21-2007, 05:35 PM
Does anyone know how you could go about writing a getElementByClass() for flash.

Chris_Seahorn
01-21-2007, 06:44 PM
Being more appropriate to the Javascript forum than the XML forum, I'm going to move this over so the experts in there can hook you up. :thumbsup:

aversion
01-21-2007, 07:23 PM
Do you want javascript? There aren't classnames in actionscript. If you want something in AS this should be in the AS forum. But:

There are a lot of Javascript libraries out there, like jQuery (http://jquery.com/), that use functions like this and there are lots of examples of functions you can just drop into your own scripts.

Here's one from Robert Nyam (http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/)



/*
Written by Jonathan Snook, http://www.snook.ca/jonathan
Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\\-");
var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
var oElement;
for(var i=0; i<arrElements.length; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}

JPnyc
01-21-2007, 10:20 PM
That's if you need to check ALL elements for a given class. If all the elements of that given class are of one tag, all you need is:

var Aobj = document.getElementsByTagName('a');
for(var i=0; i<Aobj.length;i++) {
if(Aobj[i].classname=='whatever') {
//do stuff to them here

}
}

ericflash
01-22-2007, 12:49 AM
Hi and thanks for your reply. I'm trying to make this work with flashes XML parser as it doesn't already have one. Is there a way to do this.

aversion
01-22-2007, 06:30 AM
What is it that you need to do exactly? Find classnames or tags? Are you writing this in AS? If so what elements are you looking for?

There are many ways to parse XML in flash, and there's a whole forum here that can help you with it. I imagine your post should be moved back there but you will have to explain what it is you want to do.

Chris_Seahorn
01-22-2007, 07:56 AM
Completely lost now as to what you are trying to do and what data you are trying to do it with.


Hi and thanks for your reply. I'm trying to make this work with flashes XML parser as it doesn't already have one. Is there a way to do this.

What is "this"?

As in I'm trying to make "this" work. You have given neither an example of the data you are grabbing nor any examples or mockups or previous attempts either through XML or Flash (.fla). Until this moment I moved this thread you simply stated:


Does anyone know how you could go about writing a getElementByClass() for flash.

which is most definitely javascript. Seeing as we have a newly created Javascript forum and the question is seemingly related to that language, it made sense to send it in here.

So what is your data and where are your current mockups (what I assume is "this" in your reference) of whatever you are trying to do that brought that question up? We can't help if we are confused. Thanks in advance :thumbsup:

aversion
01-22-2007, 09:53 AM
Yeah, I think it is javascript he's looking for, unless it's XHTML being imported into flash and he's looking for a way to parse that based on class names in the XHTML.

Chris_Seahorn
01-22-2007, 10:15 AM
I'm no authority on JavaScript but I do understand scripting and looking at both the example aversion posted as well as JPnyc's they are basically constructing an array which with a bit of additional setup could be sent into flash any way you desire. This in and of itself doesn't make it necessarily XML related but could be handled in the myriad of ways flash can handle arrays if sent in correctly (either natively or constructed through concatenation). Problem for us is much like your backend we have no idea what you plan to do on the frontend or populate with the data.

If it for some reason has to be sent in as XML you would build the xml by starting the root element (firstChild) before the "for" loop, concatenating the data into valid nodes (childNodes...either element style or attribute style) inside the "for" loop and closing the root element after the "for" loop and either writing it to disk and sending in the written xml file name with something like a FlashVars variable (or other straight forward method depending on what this main file is) or by actually hitting the file this JavaScript routine resides in and making it seem like inline XML to Flash (like how a lot of RSS backends are read from MySQL queries....which actually reside in php files but to flash the end result is it thinks it's hitting an XML file). Even so...I don't see why that would be beneficial because looking at those scripts you can construct the array data anyway you want, none of which requires XML or the use of an XML object in flash and send it in with less scripting while providing the same benefit on the frontend.

I'm still curious to know what made you think Flash cannot parse XML and why you feel you need this javascript routine to be tied to XML.

I guess what I'm saying is yes, Flash can parse XML, but it has to be valid XML which we then normally bust down into arrays when it's parsed (be they standard arrays, associative or multidimensional) and then dissemilate and repurpose that data within in our movies. This still requires valid XML to be sent in or read from, none of which is done with either script above. Those scripts create arrays of data.....a completely different method on face value. :)

ericflash
01-22-2007, 11:32 AM
Ok I知 sorry it was late last night and I was working on a project and was very frustrated and tired. I didn't realize it would rub on everyone in a simple flash thread. So please calm down and I really do apologize for not describing myself properly.

Chris_Seahorn thanks for moving this thread over as it applies to both forums.

I have an RSS feed which has some xml tags and then inside those tags it has a content:encoded tag wrapped with a CDATA tag which has quite a bit of xmlns inside the CDATA. The problem is drilling down through a bunch of <td> tags inside the content:encoded, CDATA tag to get to a class tag. Flash will start to bog down on large RSS feeds if I take the approach of parsing through all those <td> tags.
So I know there is a function in javascript that is called getElementByClass()
So I wanted to know first if it could be adapted to flash and if so has anyone try to do this.

Again I知 sorry if I wasn't so clear last night.

This is a chopped out example of what the xmlns content looks like.

<content:encoded xmlns:content="http://stef/crock/tt/tenba/"><![CDATA[<div id="productView"><table class="stamp" style="background-color: #FFFFFF; border: 1px solid #E4DEE0; margin-top: 5px; padding: 2px; width: 250px; font-family: verdana; font-size: 11px;"><td style="padding: 2px; background-color: #0E87C0; color: white; text-align: center;" colspan="2" class="product_bottom"><a title="download this product. Right-click for prodMenu" href="http://www.spenser.com/download.html?prodColl=product&amp;prodKey=BTXZZTpI5Od l2ciye_L06XXX IASIAAScnwtvYmtws0333443sdsd" style="color: white; text-decoration: underline" id="product" class="downloadProductLink">product!</a></td></tr></table></div>]]></content:encoded></item>

This is just a snippet and some of the class tags are embedded in quite a drill down of <td> tags as I said above. Please don't use this example above to test as it's just hacked from a large section of content. I just din't think it would be of importance to paste it all into this forum for a answer to my question.

Chris_Seahorn
01-22-2007, 12:00 PM
This is a common structure (colon seperated tags) used in weather feeds and such. Go to my forum and search for "weather" and you will get all kinds of examples (some with scripts and flas) from users as well as previous mods on busting down these types of feeds (not specifically weather feeds but how to bust a feed with colon seperated tagging using the same principles you will find in these weather feed examples stored here) . Flashkit is such a large forum and has so many thousands of posts archived that sometimes without knowing what word or words to search for the gold gets passed by in a 500 post search return. Searching for "weather" will narrow it right down for your type of XML. :thumbsup:


If you are already familiar with busting down this type of feed and feel javascript will speed it up or help keep it from bogging , I will have to leave it to the javascript gurus in here to run with that directive. Even so, if javascript is used to preprocess the feed, what it filters out would still need to be restructured as valid XML like the original. Even though Flash has limitations on size (since it times out on large XML...although AS3 is getting better at it) your original is still valid XML and the end result of the javascript processing will have to be valid (in any form) XML as well. :)

ericflash
01-22-2007, 12:17 PM
Hi Chris_Seahorn,

Thanks so much for your reply.
When you say " Go to my forum and search for "weather" " you mean the xml forum right.
I just went there and found a few areas to look in so I値l take your advice and go through these.

I more used to flash but just thought it might not be that hard to incorporate a method like getElementByClass() into flash, but by the sounds of things it would be quite hard.

My main concern is flash bogging down trying to parse through all the <td> tags to get to that one class inside it.

As I said I値l have a look at the links I found and if I don't find anything I値l try and post it another way in the XML forum.
If that's ok.

Thanks,
Again

Chris_Seahorn
01-22-2007, 12:20 PM
Yeah...there will be examples in my forum. Read back one post...I see what you are doing. As long as the preprocessing by javascript results in valid XML as the end result (either through concatenation to rebuild a new XML to be read or similar)....I see where you are headed and think it is worth exploring. In that case I will leave it to the Javascript gurus since they are the ones to know best what can be done in that context. Good luck :thumbsup: :thumbsup:

Chris_Seahorn
01-22-2007, 12:33 PM
As I said I値l have a look at the links I found and if I don't find anything I値l try and post it another way in the XML forum.
If that's ok.


Of course :)

Had I known more info at the time I would have left it there. On face value it seemed more fitting for in here at the time. I know that since forums like mine are a combination of frontside and backside routines, if you post one entire XML (like you are dealing with) file along with the actionscript you use now to bust it, it will speed it up for you. My XML forum users are very helpful and do some wonderous things if given the information they need (and I've been known to solve a few from time to time too :)).