00:00
00:00
Newgrounds Background Image Theme

Johndewey just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Xml -not Slice But Direct Ref | As3

464 Views | 5 Replies
New Topic Respond to this Topic

I've got xml which basically is formatted like this:

<content>
<headline>
Planet | Earth | Country | UK | Name | John | Job | Sales | Hobby | Fishing
</headline>
</content>

I'd like something like this:

if (PickContent["Name"] == 'John') {

Right now I'm using the slice like this:

var pickContentName:Array;
pickContentName = pickContentName.slice(5,6);

but to have it something like this would be much better for me:

var pickContentName:Array
pickContentName = pickContent[name]
So "pickContentName = John"

So basically, having it so that I can reference the content, not from where it is in the XML but what the name is called. The way I have it right now, if I had more things to the XML before [5,6] everything will go out of order.

Prior to this code, I do have split so that's already in the bag.

Response to Xml -not Slice But Direct Ref | As3 2014-10-28 14:02:41


Look at String.split(). It returns an array based on what you have specified the delimiter as. For example, if I have 5 items separated by commas:

var str:String = "a,b,c,d,e"

then doing str.split(",") with "," as the delimiter/separator will give me an Array:

var arr:Array  = str.split(",")
trace(arr) //returns [a, b, c, d, e]
trace(arr[3]) //returns "d"

Your separator here will be the vertical bar |, although you shouldn't have spaces in it otherwise it wouldn't match exactly.


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature

Response to Xml -not Slice But Direct Ref | As3 2014-10-28 15:01:18


The purpose of xml is to do that kind of stuff for you. Instead of separating the data yourself, make xml do it for you.

<headline>News</headline>
<headline>Sports</headline>

Now you have an xmllist of headlines. http://www.republicofcode.com/tutorials/flash/as3xml/

Response to Xml -not Slice But Direct Ref | As3 2014-10-28 18:59:47


At 10/28/14 02:02 PM, Gimmick wrote: var str:String = "a,b,c,d,e"

then doing str.split(",") with "," as the delimiter/separator will give me an Array:

My slice worked because I had already split it.


although you shouldn't have spaces in it otherwise it wouldn't match exactly

Ah yeah, my bad... they actual xml doesn't have any spaces between the vertical bars.

At 10/28/14 03:01 PM, MSGhero wrote: The purpose of xml is to do that kind of stuff for you. Instead of separating the data yourself, make xml do it for you.

I am doing it this way because I have nothing to do with how this XML has been created.

Response to Xml -not Slice But Direct Ref | As3 2014-10-28 20:35:42


At 10/28/14 06:59 PM, Aprime wrote: I am doing it this way because I have nothing to do with how this XML has been created.

Then you can't access elements by name unless you create a dictionary or object map and do it manually. You know the odd elements are the keys and evens are the values, so that makes it a bit easier if you can't make the xml any more useful.

Response to Xml -not Slice But Direct Ref | As3 2014-10-29 12:32:36


At 10/28/14 08:35 PM, MSGhero wrote: object map

Great, thanks!