create an array from external xml file

  • susancbk
  • Proficient
  • Proficient
  • susancbk
  • Posts: 293
  • Loc: New York City

Post 3+ Months Ago

im attempting to build a photo viewer, and the first step was to create an array in flash which would read the photo names from an external xml file

here is the code i have but its not tracing anything-


Code: [ Select ]
var xmlDoc:XML = new XML();
System.useCodepage = true;
xmlDoc.ignoreWhite = true;

xmlDoc.onLoad = function(ok:Boolean) {
  if (ok) {
    doMenu(this);    
  } else {
    trace("XML did not load");
  }
};

xmlDoc.load("myImages.xml");

function doMenu(xml:XML) {
  var num:Number = xml.firstChild.childNodes.length;  
  pics = new Array();  
  for (var i = 0; i < num; i++) {
    pics[i] = xml.firstChild.childNodes[i].attributes.image;    
  }
  trace(pics);  
}
  1. var xmlDoc:XML = new XML();
  2. System.useCodepage = true;
  3. xmlDoc.ignoreWhite = true;
  4. xmlDoc.onLoad = function(ok:Boolean) {
  5.   if (ok) {
  6.     doMenu(this);    
  7.   } else {
  8.     trace("XML did not load");
  9.   }
  10. };
  11. xmlDoc.load("myImages.xml");
  12. function doMenu(xml:XML) {
  13.   var num:Number = xml.firstChild.childNodes.length;  
  14.   pics = new Array();  
  15.   for (var i = 0; i < num; i++) {
  16.     pics[i] = xml.firstChild.childNodes[i].attributes.image;    
  17.   }
  18.   trace(pics);  
  19. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

attributes is case sensitive... in your XML file is 'image' all lowercase like that?


Other than that... everything looks like it should be working correctly.

Unless it is a datatype issue, in which case I can't help you because I use Flash MX, not 2004.
  • susancbk
  • Proficient
  • Proficient
  • susancbk
  • Posts: 293
  • Loc: New York City

Post 3+ Months Ago

here is my xml file

Code: [ Select ]
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<MENU>
  <ITEM image="image1.jpg" />
  <ITEM image="image2.jpg" />
  <ITEM image="image3.jpg" />
  <ITEM image="image4.jpg" />
</MENU>
  1. <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
  2. <MENU>
  3.   <ITEM image="image1.jpg" />
  4.   <ITEM image="image2.jpg" />
  5.   <ITEM image="image3.jpg" />
  6.   <ITEM image="image4.jpg" />
  7. </MENU>


i'm also using flash mx

there's no output when I publish...shouldnt it trace the array?
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Well that is odd.... I copied your exact actions and saved the file. Then I copied your exact XML and saved the file.

After previewing the Flash movie, the output window gave me
Quote:
image1.jpg,image2.jpg,image3.jpg,image4.jpg
  • susancbk
  • Proficient
  • Proficient
  • susancbk
  • Posts: 293
  • Loc: New York City

Post 3+ Months Ago

how weird. i ever started a .fla from flash to see if my other stuff had been causing the problem.

i put the xml in the first frame, the xml is saved in the same directory... flash mx ... not 2004.... wonder what could be causing this?
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Oh, if you are using MX and not MX2004 then that is your problem.

MX doesn't use strict datatyping, that came around in MX2004.

So variables typed as var xmlDoc:XML and ok:Boolean and such shouldn't... it should just be xmlDoc, and ok, etc.

Code: [ Select ]
var xmlDoc = new XML();
System.useCodepage = true;
xmlDoc.ignoreWhite = true;
xmlDoc.onLoad = function(ok) {
    if (ok) {
        doMenu(this);
    } else {
        trace("XML did not load");
    }
};
xmlDoc.load("myImages.xml");
function doMenu(xml) {
    var num = xml.firstChild.childNodes.length;
    pics = new Array();
    for (var i = 0; i<num; i++) {
        pics[i] = xml.firstChild.childNodes[i].attributes.image;
    }
    trace(pics);
}
  1. var xmlDoc = new XML();
  2. System.useCodepage = true;
  3. xmlDoc.ignoreWhite = true;
  4. xmlDoc.onLoad = function(ok) {
  5.     if (ok) {
  6.         doMenu(this);
  7.     } else {
  8.         trace("XML did not load");
  9.     }
  10. };
  11. xmlDoc.load("myImages.xml");
  12. function doMenu(xml) {
  13.     var num = xml.firstChild.childNodes.length;
  14.     pics = new Array();
  15.     for (var i = 0; i<num; i++) {
  16.         pics[i] = xml.firstChild.childNodes[i].attributes.image;
  17.     }
  18.     trace(pics);
  19. }


That should work.
  • susancbk
  • Proficient
  • Proficient
  • susancbk
  • Posts: 293
  • Loc: New York City

Post 3+ Months Ago

ahh i didnt mention the version at first because you said you were using mx so i assumed if you worked for you it work for me

Quote:
Unless it is a datatype issue, in which case I can't help you because I use Flash MX, not 2004.



what you gave me works great- thanks so much!
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

I removed the strict datatyping for it to work in MX. I figured if it worked in MX without the datatyping, then it should work in MX2004 with the datatyping, unless or course there was an error with the datatyping, in which case as I stated I wouldn't be able to help :)

Sorry, that was my fault, since I said I used your exact actions when testing... when I actually didn't.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 12 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.