XML Class

I’m looking at the XML class.

I would like to be able to access elements by name and the order they came in.

For example, if an xml document is structured like this:

<a>
<b>info</b>
<d>something</d>
<b>more info</b>
</a>

I would like to get at it by doing somthing like:

xml_ojbect.findChild(“a”, 1).findChild(“b”, 2).nodeValue;
//returns the first element of type “a” and the second element of type “b” encountered.

From where I stand it doesn’t look like there is any way to traverse an XML document by giving a path using names.

For example, using Flash’s XML, I would have to do something like:

xml_object.childNodes[0].childNodes[2].nodeValue;

I have a problem if I can’t access nodes by the named type of the element.

For example, if the XML document is structured

<a>
<b>info</b>
//element <d> is not in this document
<b>more info</b>
</a>

the code: xml_object.childNodes[0].childNodes[2].nodeValue won’t work but xml_ojbect.findChild(“a”, 1).findChild(“b”, 2).nodeValue will work.

Another example:

xml_object.childNodes.length will return the number of childnodes there are. But I don’t see a way to get at the number of childnodes of a particular type. This will not tell me, for example, how many childnodes of type “b” that “a” has.

I can’t see Flash XML support being this sparse. Is there a built-in way of accessing the nth child of a node of a particular element name or am I stuck with accessing it only by number.

//ActionScript:
XMLNode.prototype.findChild = function(name, index){
	var count = 0;
	var currNode = this.firstChild;
	do{
		if (currNode.nodeName == name){
			count++;
			if (count == index) return currNode;
		}
	}while (currNode = currNode.nextSibling);
	return false;
}

though I think your example is a bit off. It should read:

xml_ojbect.findChild("a", 1).findChild("b", 2).firstChild.nodeValue;

if you’re after “more info” as b itself has no node value

blech…I was afraid of that. Here’s what I’ve done so far to meet my needs. I actually might have the need to completely revamp the XML class. It is so poor as it stands (jeez - they don’t even provide a way to traverse a document by nodename!) I’m suprised a quick google search wasn’t able to find someone who already did it. I’ll share my code if I do more.

If I do completely rework it, it will be similar to XSL - both in terms of functionality and use.

I named it MyXML simply because I don’t know a dern thing about how flash handles namespacing. Another quick googlesearch on namespacing yielded nothing.

// ActionScript Document
class MyXML extends XML {
 function numChildren(nodeName):Number {
  //returns the number of first generation children of type nodeName
  var numFound:Number = 0;
  for (var i=0; this.childNodes && i < this.childNodes.length; i++) {
   if (this.childNodes*.nodeName eq nodeName) {
	numFound++;
   }
  }
  return numFound;
 }
 function numSameSiblings():Number {
  //returns the number of siblings of the same type as the current node
  var numSiblings:Number = 0;
  for (var i = 0; i < this.parentNode.childNodes.length; i++) {
   if (this.parentNode.childNodes*.nodeName eq this.nodeName) {
	numSiblings++;
   }
  }
  return numSiblings;
 }
 function findChild(nodeName, number):MyXML {
  //returns the nth child of type nodeName.  If no such node exists, null is returned
  var numFound = 0;
  for (var i=0; this.childNodes && i < this.childNodes.length; i++) {
   if (this.childNodes*.nodeName eq nodeName) {
	numFound++;
	if (numFound == number) {
	 return this.childNodes*;
	}
   }
  }
  return null;
 }
 
 function isChild(nodeName):Boolean {
  //returns true if the current node is a child with a direct parent of type nodeName
  for (var i=0; this.childNodes && i < this.childNodes.length; i++) {
	  if (this.childNodes*.nodeName eq nodeName) {
	   return true;   
	  }
	 }
	 return false;
 }
}//end class MyXML

hrmph…code doesn’t seem to want to format neatly…

Flash (developers) try to maintain a small file size as a plugin download. To keep this size, they keep the code base pretty limited. There are places that provide additional XML methods, http://proto.layer51.com/ being one of them, though I think most people seem to get by. I don’t think Flash has the horsepower to really deal with too much XML work anyway. Those who need it to, usually make the methods needed to get the job done (though I too am surprised more werent found on a google search).
:-/

Here some similar code but a little shorter,


XMLNode.prototype.__resolve = function (name) {
	var node;
	for (var i in this.childNodes) {
		node = this.childNodes *;
		if (node.nodeName == name) {
			return node;
		};
	};

	return null;
};

// usage
var xmlObj = new XML ("<root><flash>foo</flash><test>foo</test></root>");
// to get at the flash node
trace (xmlObj.firstChild.flash);
// to get at the test node
trace (xmlObj.firstChild.test);