Dynamically create xml doc from an array help, please

I have a delimited string variable that I split into an array…

What I would like to do is create an xml playlist from that array…

an example xml doc I would like to have as an end result is this:


<playlist>
	<song>
		<name>Some Song.mp3</name>
		<location>C:\Windows\Music\Some Song.mp3</location>
	</song>
	<song>
	 ......
	</song>
and so on....
</playlist>

I started with this, but Im horrid with loop statements, my brain just doesnt work that way at the moment…


function createXMLPlaylist() {
var doc:XML = new XML();
var playList:XMLNode = doc.createElement("playlist");
var arrFiles:Array = fileList.split("#");
var song:XMLNode = doc.createElement("song");
var name:XMLNode = doc.createElement("name");
var location:XMLNode = doc.createElement("location");
doc.appendChild(playList);
for (i=0; i<arrFiles.length; i++) {
var title:XMLNode = doc.createTextNode(arrFiles*);
var locale:XMLNode = doc.createTextNode(myFolder + "\\" + arrFiles*);
playList.appendChild(song);
song.appendChild(name);
song.appendChild(location);
name.appendChild(title);
name.appendChild(locale);
}
fileName = "playlist.xml";
outXML = doc.toString();
savetofile(fileName, outXML);
}

variable “myFolder” and “savetofile” are defined earlier within another function, and this function is called back to the first function which establishes the myFolder variable…oh yea, and fileList is the delimited string variable

the second set of code produces an xml doc, but only populates with <playlist /> :frowning:

any xml wiz’s wanna take a stab at this one? please ?

nevermind this one too, Ive got this one figured out, if you wanna know how its done, lemmi know…

You are copying or adding the child elements and text nodes to the same song node. You need to create a different song node for each item in the arrList, and then append it to the doc xml object. Here’s the code,


var fileList = "file1.mp3#file2.mp3#file3.mp3";

function createXMLPlaylist () {
	var doc = new XML ();
	var playList = doc.createElement ("playlist");
	doc.appendChild (playList);

	var arrFiles = fileList.split ("#");
	var n = arrFiles.length;
	var song, name, location, nameValue, locationValue;

	for (var i = 0; i < n; ++i) {
		name = doc.createElement ("name");
		nameValue = doc.createTextNode (arrFiles *)
		name.appendChild (nameValue);

		location = doc.createElement ("location");
		locationValue = doc.createTextNode ("myFolder/" + arrFiles *);
		location.appendChild (locationValue);

		song = doc.createElement ("song");
		song.appendChild (name);
		song.appendChild (location);

		playList.appendChild (song);
	};

	trace (doc);

	// xml is ready, do save stuff...
};

createXMLPlaylist ();

arcaRox - nice one, this looks a little cleaner than what I wrote. thanks :slight_smile:

however, the code that works for me, is close to this, except I have the XMLNode variables within the loop, for some reason when the node variables are defined outside the loop my saved xml file returns undefined…


var arrFiles:Array = files.split("#");
var folderPath:String = new String();
folderPath = (folder + "\\");
var doc:XML = new XML();
songlist_lb.removeAll();
for (i=0; i<arrFiles.length; i++) {
songlist_lb.addItem(arrFiles*);
var song:XMLNode = doc.createElement("song");
var songName:XMLNode = doc.createElement("songName");
var songLocation:XMLNode = doc.createElement("songLocation");
var name:XMLNode = doc.createTextNode(arrFiles*);
var location:XMLNode = doc.createTextNode(folderPath + arrFiles*);
doc.appendChild(song);
song.appendChild(songName);
song.appendChild(songLocation);
songName.appendChild(name);
songLocation.appendChild(location);
//trace(doc);
// do my xml saving stuff
}