I am building a schedule for a festival, but I’m having a bit of trouble loading the xml data into the text fields. I understand the correct way to format the xml, and how to display the information from a node into a text field, but I am having trouble getting multiple nodes to display in corresponding multiple text fields. Here is the structure of the xml file:
<?xml version="1.0"?>
<stage1>
<timeslot>
<time>9:30</time>
<band>Band name 1</band>
</timeslot>
<timeslot>
<time>10:15</time>
<band>Band name 2</band>
</timeslot>
</stage1>
<stage2>
<timeslot>
<time>9:30</time>
<band>Band name 1</band>
</timeslot>
<timeslot>
<time>10:15</time>
<band>Band name 2</band>
</timeslot>
</stage2>
In order to send that information into a single set of text fields - I’ll call them time_txt and band_txt - I would use something like the following AS:
[AS]
function loadXML(loaded) {
if (loaded) {
_root.time = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
_root.band = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
time_txt.text = _root.time;
band_txt.text = _root.band;
} else {
trace(“file not loaded!”);
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load(“schedule.xml”);
[/AS]
In order to display all the xml data, I would have to write a very long code - note that the xml example I showed is just a short example and there are over 200 bands playing the festival - indicating which node should be sent to which text field. But I’m sure there is a much better way to do this. Please help if you can.
[Hi Salimai,
From what I understood from your explaination, you wanted data from your xml to be displayed in two text boxes time_txt and band_txt respectively.
I tried to do something so that you could trace the value from the xml childnodes. I changed your xml and added <root>…</root>. Its better to write that way.
<?xml version="1.0"?>
<root>
<stage>
<timeslot>
<time>9:30</time>
<band>Band name 1</band>
</timeslot>
<timeslot>
<time>10:15</time>
<band>Band name 2</band>
</timeslot>
</stage>
<stage>
<timeslot>
<time>9:30</time>
<band>Band name 1</band>
</timeslot>
<timeslot>
<time>10:15</time>
<band>Band name 2</band>
</timeslot>
</stage>
</root>
I would suggest you to use FOR loop within your xml childnodes to trace the values. Looks like you are familiar with xml.
[AS]
//xml part
xmlData = new XML ();
xmlData.ignoreWhite = true;
xmlData.onLoad = function (success)
{
if (success)
{
loadXML (xmlData);
//trace(xmlData);
}
else
{
trace (“XML Loading failed!”);
}
};
xmlData.load (“schedule.xml”);
function loadXML (xml_doc)
{
//trace(xml_doc);
//trace(xml_doc.firstChild.childNodes.length);
for (var i = 0; i < xml_doc.firstChild.childNodes.length; i++)
{
trace (xml_doc.firstChild.childNodes*.firstChild.childNodes[0].firstChild.nodeValue);
}
}
[/AS]
I hope that will work. If not let me know. If you would explain in more details what exactly you wanted to accomplish, it would be easier to help you.
And thank you Rabi as well! I only had a few minutes to work with that code, but it definitely was a step in the right direction and I think I know what to do from here.
I suppose the point I am most unsure about right now is how to take the data that is now being traced successfully, and send it to dynamic text fields. Or rather, that point isn’t so difficult as having them sent to multiple sequential text fields. For example having the time information sent to a set of text fields named time_txt with a number following (time_txt1, time_txt2, time_txt3, etc). So the first time listed in the xml document would be sent to time_txt1, the second to time_txt2, and so on.
I have a few ideas on how to do this (again pressed for time, sorry) but there is probably a better way anyway. Could anyone point me in the right direction please?
Actually, thank you so much for your help but I found a better way to do what I’m trying to do. This would have been a huge ordeal for something that can be achieved much more easily.