Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

XML: convert XMLList to Array or comma delimited list

Guest
Oct 07, 2008 Oct 07, 2008
Consider this XML:

var xml:XML =
<xml>
<thing id="1">one</thing>
<thing id="2">two</thing>
<thing id="3">three</thing>
<thing id="4">four</thing>
<thing id="5">five</thing>
<thing id="6">six</thing>
<thing id="7">seven</thing>
</xml>

I want to turn it into this:

"one,two,three,four,five,six,seven"

Or this:

["one","two","three","four","five","six","seven"]

What's the simplest way? Ideally I'd like to do it in one line, if it's not too obtuse.
TOPICS
ActionScript
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 07, 2008 Oct 07, 2008
interesting exercise. can i kick off with the fairly straightforward(but possibly verbose, considering the power of E4X) three line technique? ( a curly bracket doesn't count as a line does it?)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 07, 2008 Oct 07, 2008
will an XMLList suffice or are you after only an Array or a String?
This will give you what you're after if an XMLList is sufficient:

var xmlList:XMLList=xml.thing.text();

if what you're after is an Array, i've struggled a little to convert an XMLList to an Array. I have found that when converting it to a string with toXMLString(), flash inserts a newline between elements, so (as long as there are no newlines in your data), you could use this to convert to an Array:

var array:Array=xml.thing.text().toXMLString().split("\n");
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 07, 2008 Oct 07, 2008
var xmlList:XMLList=xml.thing.text();

Man, so close. I just need commas or any delimiter. I don't really need an array, I need comma delimiters (which of course would be easy to generate if I had an array, and the other way around.)

toXMLString lets me break on newlines, but I need to get rid of the node syntax and just have the text left over. .

Looping works in two lines if I must. 🙂
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 07, 2008 Oct 07, 2008
if you need a comma delimited string, it's getting a little ridiculous, but you could always convert the array i created previously to a string with:
var string:String=xml.thing.text().toXMLString().split("\n").join(",");

Alternatively, you could use a regExp on the string:
var string:String=xml.thing.text().toXMLString().replace(/\n/g,',');

With the xml example you gave, and either of the above two lines of code, string would then contain:
one,two,three,four,five,six,seven

OT: spooky this is my 666th post
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 08, 2008 Oct 08, 2008
LATEST
var string:String=xml.thing.text().toXMLString().split("\n").join(",");

Bingo, ridiculous is my style, thanks. 🙂
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines