View Full Version : XML Help
I am tring to create a 2 day forecast in flash based on an xml feed. I can get the data loaded but I don't know how to sort it.
Example:
I can get the first set of data to load into dynamic text boxes but how would I get the 2nd set to load into dynamic text boxes even though some of them have the same node value.
xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<weather ver="2.0">
<head>
<dayf>
<day d="0" t="Monday" dt="Mar 31">
_ <hi>N/A</hi>
_ <low>36</low>
_ <sunr>6:27 AM</sunr>
_ <suns>6:58 PM</suns>
</day>
<day d="1" t="Tuesday" dt="Apr 1">
_ <hi>67</hi>
_ <low>44</low>
_ <sunr>6:26 AM</sunr>
_ <suns>6:59 PM</suns>
<part p="d">
_ <icon>34</icon>
_ <t>Mostly Sunny</t>
</day>
</dayf>
</weather>
Here is the code in flash:
myXML = new XML;
myXML.onLoad=handleLoad;
myXML.load("weatherl.xml");
//This is the info that I need
Thanks in advanced for your help.
catbert303
04-01-2003, 07:47 AM
You can pick all the values out from the document like this,
data_xml = new XML();
data_xml.ignoreWhite = true;
data_xml.onLoad = function(success) {
if (!success) {
trace("could not load XML");
}
var days = this.firstChild.childNodes[0].firstChild.childNodes;
for (var i = 0; i < days.length; ++i) {
trace(i + " " + days[i].attributes.t + " " + days[i].attributes.dt);
var day_info = days[i].childNodes;
for (var j = 0; j < day_info.length; ++j) {
if (day_info[j].nodeType == 1) {
trace("name: " + day_info[j].nodeName + ", value: " + day_info[j].firstChild);
if (day_info[j].hasChildNodes()) {
var part_info = day_info[j].childNodes;
for (k = 0; k < part_info.length; ++k) {
if (part_info[k].nodeType == 1) {
trace("name: " + part_info[k].nodeName + ", value: " + part_info[k].firstChild);
}
}
}
}
}
}
}
data_xml.load("weather.xml");
You could maybe use createTextField to display the values in the movie
If I had two dynamic text boxes on stage, one named tempDay1 and the 2nd named tempDay2 how would I set up the code to get the values to post inside those boxes.
Thanks Again
catbert303
04-01-2003, 01:59 PM
Hi,
you could try something like this,
data_xml = new XML();
data_xml.ignoreWhite = true;
data_xml.onLoad = function(success) {
if (!success) {
trace("could not load XML");
}
var days = this.firstChild.childNodes[0].firstChild.childNodes;
for (var i = 0; i < days.length; ++i) {
_root["tempDay" + (i + 1)].text = days[i].attributes.t + " " + days[i].attributes.dt + newline;
var day_info = days[i].childNodes;
for (var j = 0; j < day_info.length; ++j) {
if (day_info[j].nodeType == 1 && day_info[j].nodeName != "part") {
_root["tempDay" + (i + 1)].text += day_info[j].nodeName + ", value: " + day_info[j].firstChild + newline;
} else if (day_info[j].nodeType == 1 && day_info[j].nodeName == "part") {
var part_info = day_info[j].childNodes;
for (var k = 0; k < part_info.length; ++k) {
if (part_info[k].nodeType == 1) {
_root["tempDay" + (i + i)].text += part_info[k].nodeName + ", value: " + part_info[k].firstChild + newline;
}
}
}
}
}
}
data_xml.load("weather.xml");
Which will display earch name value pair retrieved on a line in the textbox for the particular day (where the textboxes are on the main timeline and have the instance names tempDay1 and tempDay2)
stevelux
02-22-2005, 04:48 PM
I have a problem with something similar and can't get it to work.
in the .onLoad using _root.myInstance OR _root["myInstance"] can trace a level just fine.
Using _root.myInstance.text = (whatever XML node name/value, etc.) doesn't work.
Neither does
_root["myInstance"].text
It's like it just can't give up any data to an external source from within the onLoad().
Anyone find a solve to this?
catbert303
02-23-2005, 08:16 AM
Can you post an fla file? there should (hopefully :)) be no problems writing information to anything from within an onLoad event handler.
stevelux
02-23-2005, 01:28 PM
I was trying to manipluate it outside the XML Object altogether. What I did is just write a function external to the XMLObj and then called it within the onLoad XML method.
Many noobs (like me) to XML are trying to do this thinking the XML object is global and we can hit the data from any frame or movieClip. Someone posted another thread where he recommended declaring a _global for the XML Object and putting it in there.
If you leave about a 5 frame delay it works :)
So, I figured it out so far. Thank you for replying to this post.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.