PDA

View Full Version : XML Parsing -> Array


rogue
01-26-2004, 01:04 AM
Hi,

I am having a problem with my xml and was hoping that someone could help me out. I am parsing my xml and as I do so, am adding the elements to an array. This array is then added to an object that was created for the parsed xml. What happens is that, the inside the parsing function, the actionscript checks for something (in the xml) with an if statement. If that condition is met an array is created and the parse fxn is called again. In this I pass along the node value and the array name. However, the array is only tracing out the last element (I am testing with 2). The first node value is being replaced by the second. Does anyone know how to correct this? I know this sounds a little vague, but I don't have the code with me, I can post it tomorrow morning. But if anyone has any ideas, I'd appreciate any help. Thanks!

CyanBlue
01-26-2004, 01:14 AM
Howdy and Welcome... :)

I think at least 70% of the newly asked questions are already answered within the forum... You'll have to search for it though and the vBullein has nice search feature, so we'd have to use it more often... :)

Try this thread and there should be more out there...
http://www.actionscript.org/forums/showthread.php3?s=&threadid=38433

rogue
01-26-2004, 11:59 AM
Thanks. I already did a search but I think I will take another look just in case I missed something :) I'll post the code here anyways just to see if anyone has any ideas - I don't think it's that hard a problem but I am just sick of looking at this :rolleyes:


function ParseNode(node, targetObj)
{
trace(node);
trace(newline);
for (var i = 0; i < node.childNodes.length; i++)
{
if(node.childNodes[i].childNodes.length > 1)
{
if(node.childNodes[i].nodeName == node.childNodes[i+1].nodeName)
{
var newChild = targetObj[node.childNodes[i].nodeName] = new Array();
for(var i = 0; i<node.childNodes[i].childNodes.length; i++)
{
trace(node.childNodes[i].firstChild.firstChild.nodeValue);

ParseNode(node.childNodes[i], newChild);
}
traceObj(newChild);
}
else
{
var newChild = targetObj[node.childNodes[i].nodeName] = new Object();
ParseNode(node.childNodes[i], newChild);
}
}
else
{
targetObj[node.childNodes[i].nodeName] = node.childNodes[i].firstChild.nodeValue;
}
}
return;

divarch
01-26-2004, 12:41 PM
Is it working??, cause my interpreter is displaying some errors, above all, an 'else' without 'if'???

Perhaps it's because you retyped it here, but maybe would be better to provide zipped .fla and .xml.

rogue
01-26-2004, 02:38 PM
This is the xml. Everything is being added to an object while the wallpaper stuff is added to an array first and then the array is added to the object. It's just adding the last element. ANY help would be appreciated! :confused:


<?xml version="1.0"?>
<configuration>
<configName>default</configName>
<movieItems>
<intro>introLogo.swf</intro>
<logo>mainLogo.swf</logo>
<banner>mainBanner.swf</banner>
<launch>launchBanner.swf</launch>
<spotlight>spotlight.swf</spotlight>
<menu>menu.swf</menu>
<toolbar>iconToolbar.swf</toolbar>
<style>style.css</style>
<news>news.css</news>
<about>about.xml</about>
<direct>direct.xml</direct>
<menuStructure>menu.xml</menuStructure>
</movieItems>
<wallpapers>
<wallpaper>
<large>flash_1.swf</large>
<thumb>flash_1_tn.swf</thumb>
</wallpaper>
<wallpaper>
<large>flash_2.swf</large>
<thumb>flash_2_tn.swf</thumb>
</wallpaper>
<wallpaper>
<large>flash_3.swf</large>
<thumb>flash_3_tn.swf</thumb>
</wallpaper>

</wallpapers>
</configuration>

yourhoast
02-08-2004, 10:38 PM
you might wanna have a look at google.com > xfactor studio xpath

Seppuku
02-09-2004, 11:01 AM
I'm by no means a flash expert, but as a programmer, that sounds like recursion. My current project involves parsing an XML tree into an array doing just that. Here is my code:


//This will store our array
var stockData = new Object;
//This stores our URL to the XML source
var stockURL = "test.xml";

//Create an XML object and set it up
stockQuote_xml = new XML();
stockQuote_xml.ignoreWhite = true;

//When this XML loads, test for success, then parse it...
stockQuote_xml.onLoad = function(success){
if(success){
processXMLData(stockQuote_xml.firstChild);
}
// An error occurred retrieving the XML...
else{
trace(stockURL);
quoteInfo._visible = false;
quoteDate.text = "Unable to load quote. (2)";
}
}

//Recursion function to populate the array
function processXMLData(xmlDoc_xml) {
//Show what node we're on
trace(xmlDoc_xml.nodeName + " = " + xmlDoc_xml.firstChild.nodeValue);

//Populate the array with the current node and it's value
stockData[xmlDoc_xml.nodeName] = xmlDoc_xml.firstChild.nodeValue;

//For each child this node has, call this function again
for (a in xmlDoc_xml.childNodes) {

if(xmlDoc_xml.childNodes[a].hasChildNodes()){
processXMLData(xmlDoc_xml.childNodes[a]);
}
}
}

//Start the ball rolling.. load the XML
stockQuote_xml.load(stockURL);


The value passed in "stockURL" is the URL to the XML source (or in my case, a Perl script that scrapes the XML from a 3rd party website since Flash won't grab XML from a different domain then the Flash was loaded from).

rogue
02-09-2004, 01:57 PM
Thanks! It was a recursion problem but I managed to figure it out. I appreciate all the input!