0

this is part of an XML file I retrieve using AS3 E4X:

<links>
    <link>
      <label>Versions</label>
      <href>http://mylink1</href>
    </link>
    <link>
      <label>Configurations</label>
      <href>http://myLink2</href>
    </link>
</links>

I want to retrieve the values of labels, so I write:

document.links.link.label.text();

This returns VersionsConfigurations. I need this as Array ([Versions, Configurations]) but I would like not to use a loop. Is there any other way?

2
  • Sorry, I found one way, and I want to share it with you all. You can do this by writing document.links.link.label.text().toXMLString().slice("\n"). The method toXMLString() adds a useful (undocumented) "\n" and you can use it to split the string. Of course this results in an XML string Commented Jul 25, 2011 at 9:08
  • You can post answers to your own questions, although it will make you wait 2 days before you can accept you own answer. Commented Jul 25, 2011 at 9:09

2 Answers 2

1

Well, this is a "don't try this at home" solution, but here you are. :)

You can use E4X search expression to do whatever you want to nodes of an XMLList.

This works as follows: someXMLList.(expression), where expression is any AS3 code that can access each node's properties and methods with no need of qualifying their names. For instance, you could do the following:

yourXML.descendants("label").(trace("label text: ", text()));

Note that I'm using text() here with no access . operations. Actually this will return an new XMLList for all nodes, where expression evaluated to true. Since trace() returns void, the resulting list will be empty. Internally there is of course a loop through all nodes of XMLLIst that is created by calling descendants() (or using .. operator).

You can construct your array the same way.

var doc:XML = 
<links>
    <link>
      <label>Versions</label>
      <href>http://mylink1</href>
    </link>
    <link>
      <label>Configurations</label>
      <href>http://myLink2</href>
    </link>
    <link>
      <label>A label
with
multiple
line 
breaks</label>
      <href>http://myLink3</href>
    </link>
</links>;

trace(doc.descendants("label").text().toXMLString().split("\n"));
/* Trace output (incorrect):
Versions,Configurations,A label
,with
,multiple
,line 
,breaks
*/

var list:Array = [];
doc.descendants("label").(list.push(text().toString()));
trace(list);
/* Trace output (correct):
Versions,Configurations,A label

with

multiple

line 

breaks
*/

That may be useful when performing some complicated searches on an XMLList. However in your case I think you should instead use simple splitting of a string representation or a regular expression as Shane suggests.

Sign up to request clarification or add additional context in comments.

2 Comments

That's interesting! One silly question more: is there any way to "compact" this 2 lines var list:Array = []; d.descendants("label").(list.push(text().toString())); in a single assignment to the list array? Say, var list = ...
Most E4X expressions return an XMLList constructed according to the rules you provide. Search expressions work this way no matter what you do inside the parentheses. What I did in the answer above is I used search expression as a shortcut to looping through XMLList items. But you still can't rely on return value and you need to create an empty array somewhere outside search expression.
0

An alternative technique could be to use a regular expression, although this particular example is dependent on your labels always starting with a capital and otherwise containing only lower case characters.

var regex:RegExp = /[A-Z][a-z]+/g;
var inString:String = "VersionsConfigurations";
var outArray:Array = inString.match(regex);
trace(outArray.length); // 2

3 Comments

Shane, I tried methods like this one, but there's a different rule for every node in this XML! The label is only one example, there are lots and every one creates different problems. I tried to propose the XML structure but it seems there's a CMS that creates problems for us the flash developers :-)
Seems your way is safer anyway, until there are newlines in your XML :)
It is the toXMLString() that adds lines

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.