XML > Array

  • bayes
  • Newbie
  • Newbie
  • bayes
  • Posts: 7

Post 3+ Months Ago

Hi there, how would I go about plugging data in an .XML file into an array?

I've got the XML and am trying to use this code to read from it, but it just prints out "undefined" all the time.

Any suggestions? Cheers


-- XML--
Code: [ Select ]
<?xml version="1.0" encoding="iso-8859-1"?>
<creatures>
    <Creature isa="Lifeform"/>
    <Mammal isa="Creature" has="hairy_skin,mouth,warm_blood"/>
    <Reptile isa="Creature" has="scaly_skin,mouth,cold_blood"/>
    <Bird isa="Creature" has="feathers,beak,wings,warm_blood"/>
    <Fish isa="Creature" has="scales,mouth,cold_blood"/>
    <Cat isa="Mammal" has="fur,tail,whiskers"/>
    <Dog isa="Mammal" has="hair,tail,wet_nose"/>
    <Tiger isa="Cat" has="stripey_fur"/>
    <Lion isa="Cat" has="brown_fur,mane"/>
    <Panther isa="Cat" has="black_fur"/>
    <Mullet isa="Fish" has="thick_lips"/>
    <Pike isa="Fish" has="sharp_teeth"/>
    <Labrador isa="Dog" has="soft_hair"/>
    <Pig isa="Mammal" has="snout,trotters"/>
    <Cow isa="Mammal" has="nose,hooves"/>
    <Lizard isa="Reptile" has="tail,legs"/>
    <Snake isa="Reptile" has="tail,no_legs,forked_tongue"/>
    <Iguana isa="Lizard" has=""/>
    <Cobra isa="Snake" has="hooded_head"/>
    <GreatCobra isa="Cobra" has="big_hood"/>
</creatures>
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <creatures>
  3.     <Creature isa="Lifeform"/>
  4.     <Mammal isa="Creature" has="hairy_skin,mouth,warm_blood"/>
  5.     <Reptile isa="Creature" has="scaly_skin,mouth,cold_blood"/>
  6.     <Bird isa="Creature" has="feathers,beak,wings,warm_blood"/>
  7.     <Fish isa="Creature" has="scales,mouth,cold_blood"/>
  8.     <Cat isa="Mammal" has="fur,tail,whiskers"/>
  9.     <Dog isa="Mammal" has="hair,tail,wet_nose"/>
  10.     <Tiger isa="Cat" has="stripey_fur"/>
  11.     <Lion isa="Cat" has="brown_fur,mane"/>
  12.     <Panther isa="Cat" has="black_fur"/>
  13.     <Mullet isa="Fish" has="thick_lips"/>
  14.     <Pike isa="Fish" has="sharp_teeth"/>
  15.     <Labrador isa="Dog" has="soft_hair"/>
  16.     <Pig isa="Mammal" has="snout,trotters"/>
  17.     <Cow isa="Mammal" has="nose,hooves"/>
  18.     <Lizard isa="Reptile" has="tail,legs"/>
  19.     <Snake isa="Reptile" has="tail,no_legs,forked_tongue"/>
  20.     <Iguana isa="Lizard" has=""/>
  21.     <Cobra isa="Snake" has="hooded_head"/>
  22.     <GreatCobra isa="Cobra" has="big_hood"/>
  23. </creatures>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • digitalMedia
  • a.k.a. dM
  • Genius
  • User avatar
  • Posts: 5149
  • Loc: SC-USA

Post 3+ Months Ago

You need to use an XML Connector from the components list. You can pull it all into an array, but it would be much simpler to bind the connector to a Dataset (also in the components list).

I should probably ask which version of Flash you're using. (?)


Is this your own XML schema?
  • bayes
  • Newbie
  • Newbie
  • bayes
  • Posts: 7

Post 3+ Months Ago

I'musing flash mx 2004, and, given the level of programming skill I possess, would have to pull it into an array. Data connectors? I wouldn't know what they were!

The XML is not mine, no.
  • bayes
  • Newbie
  • Newbie
  • bayes
  • Posts: 7

Post 3+ Months Ago

Ok, so I've got this - the trace function doesn't appear to work though - being a novice I've probably done something quite stupid, so please forgive me :)

-- Helps if I include it!

Code: [ Select ]
on (release){
    
function parseXML(creatXML) {
  parseArray = new Array();
  for (i = 0; i < creatXML.childNodes.length; i ++) {
     this.creatXML.childNodes[i].nodeName = new Object();
         trace(creatXML.childNodes[i].nodeName);
     this.creatXML.childNodes[i].nodeName.name = creatXML.childNodes[i].nodeName;
     this.creatXML.childNodes[i].nodeName.isa = creatXML.childNodes[i].attributes.isa;
     this.creatXML.childNodes[i].nodeName.has = creatXML.childNodes[i].attributes.has.split(",");
     parseArray.push(this.creatXML.childNodes[i].nodeName);
     }
  _global.creaturesArray = parseArray;
}
 
creatureXML = new XML();
creatureXML.ignoreWhite = true;

creatureXML.onLoad = function (success) {
parseXML(this.firstChild);
creatureXML.load("creatures.xml");}

}
  1. on (release){
  2.     
  3. function parseXML(creatXML) {
  4.   parseArray = new Array();
  5.   for (i = 0; i < creatXML.childNodes.length; i ++) {
  6.      this.creatXML.childNodes[i].nodeName = new Object();
  7.          trace(creatXML.childNodes[i].nodeName);
  8.      this.creatXML.childNodes[i].nodeName.name = creatXML.childNodes[i].nodeName;
  9.      this.creatXML.childNodes[i].nodeName.isa = creatXML.childNodes[i].attributes.isa;
  10.      this.creatXML.childNodes[i].nodeName.has = creatXML.childNodes[i].attributes.has.split(",");
  11.      parseArray.push(this.creatXML.childNodes[i].nodeName);
  12.      }
  13.   _global.creaturesArray = parseArray;
  14. }
  15.  
  16. creatureXML = new XML();
  17. creatureXML.ignoreWhite = true;
  18. creatureXML.onLoad = function (success) {
  19. parseXML(this.firstChild);
  20. creatureXML.load("creatures.xml");}
  21. }
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

XML loaded into Flash is automatically parsed into an array. Where the childNodes are accessable via array syntax.
Code: [ Select ]
xmlObjectName.firstChild.childNodes
will return the array of all the nodes within the first node of the XML file.
Code: [ Select ]
xmlObjectName.firstChild.childNodes[0]
Will return the first child node of the first node in the XML file (since arrays are 0 based it starts counting at 0)
  • bayes
  • Newbie
  • Newbie
  • bayes
  • Posts: 7

Post 3+ Months Ago

In my code the details have been passed to an array, parseArray, so all I need to do now is read from the array - but I'm having problems.

Whenever I try to refer to parseArray[1].isa, the output window keeps saying "undefined". What am I doing wrong? Can you not reference the array in the same way that you reference the XML object?
  • digitalMedia
  • a.k.a. dM
  • Genius
  • User avatar
  • Posts: 5149
  • Loc: SC-USA

Post 3+ Months Ago

Is this a complete sample of the XML your using?

I may be out in left field somewhere, but I've never seen XML done like this. I don't see where the data is. Only self closing tags with attributes.

I'm used to something like this

Code: [ Select ]
<lifeform>
<animal>Cat</animal>
 <characteristics>
  <type>Tiger</type>
  <markings>striped fur</markings>
 </characteristics>
</lifeform>
  1. <lifeform>
  2. <animal>Cat</animal>
  3.  <characteristics>
  4.   <type>Tiger</type>
  5.   <markings>striped fur</markings>
  6.  </characteristics>
  7. </lifeform>


I'm going to go look now.
  • UNFLUX
  • Genius
  • Genius
  • User avatar
  • Posts: 6376
  • Loc: twitter.com/unflux

Post 3+ Months Ago

i agree dM.. that doesn't even look like a real xml file to me.

and furthermore, I would take lostinbeta's advice. ;)
  • bayes
  • Newbie
  • Newbie
  • bayes
  • Posts: 7

Post 3+ Months Ago

Yeah I agree, I thought the XML was very dodgy indeed but that's what I've been given to work with and unfortunately I can't get around that.

I can retrieve the data using the ".isa" and ".has" from the XML object itself, but not once its been passed to the parseArray. I just cant seem to fathom it.
  • barrax
  • Newbie
  • Newbie
  • barrax
  • Posts: 12
  • Loc: Southampton, UK

Post 3+ Months Ago

Your xml is fine - it's all pretty simple to parse. Heres the code i used. If you got a button to run all this just use on(release){parseXML();}

Here are the two functions i use:

Code: [ Select ]
function parseXML(){
    myXML = new XML();
    myXML.load("creatures.xml");
    myXML.ignoreWhite = true;
    myXML.onLoad = function(success){
        if(success){
            trace("xml loaded...");
            makeArray();
        }else{
            trace("xml load failed...");
        }
    }
}

function makeArray(){
    parseArray = [];
    nodes = myXML.firstChild.childNodes;
    for(i=0;i<nodes.length;i++){
        creature = nodes[i].nodeName;
        isa = nodes[i].attributes.isa;
        has = nodes[i].attributes.has;
        parseArray.push({creature:creature,isa:isa,has:has});
    }
}
  1. function parseXML(){
  2.     myXML = new XML();
  3.     myXML.load("creatures.xml");
  4.     myXML.ignoreWhite = true;
  5.     myXML.onLoad = function(success){
  6.         if(success){
  7.             trace("xml loaded...");
  8.             makeArray();
  9.         }else{
  10.             trace("xml load failed...");
  11.         }
  12.     }
  13. }
  14. function makeArray(){
  15.     parseArray = [];
  16.     nodes = myXML.firstChild.childNodes;
  17.     for(i=0;i<nodes.length;i++){
  18.         creature = nodes[i].nodeName;
  19.         isa = nodes[i].attributes.isa;
  20.         has = nodes[i].attributes.has;
  21.         parseArray.push({creature:creature,isa:isa,has:has});
  22.     }
  23. }
  • barrax
  • Newbie
  • Newbie
  • barrax
  • Posts: 12
  • Loc: Southampton, UK

Post 3+ Months Ago

BTW: you read it like... mycreature = parseArray[2].creature; creatureType = parseArray[2].isa; etc etc

so you could write a function that accepts a number like:
Code: [ Select ]
function selectCreature(n){
currCreature = parseArray[n].creature;
currType = parseArray[n].isa;
}
  1. function selectCreature(n){
  2. currCreature = parseArray[n].creature;
  3. currType = parseArray[n].isa;
  4. }


so on your button that selects a creatur you have:

Code: [ Select ]
on(release){
selectCreature(2);
}
  1. on(release){
  2. selectCreature(2);
  3. }


Hope this helps

B
  • bayes
  • Newbie
  • Newbie
  • bayes
  • Posts: 7

Post 3+ Months Ago

Ah, apologies for being cretinous - I completely missed barrax's first post.

Cheers barrax and all.
  • digitalMedia
  • a.k.a. dM
  • Genius
  • User avatar
  • Posts: 5149
  • Loc: SC-USA

Post 3+ Months Ago

Hey barrax: Is there some advantage to passing data through tag attributes? I'm just curious, and I'd like to know more. If you have any info, I'd be grateful.

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

Post 3+ Months Ago

Man I missed a lot...

Bayes original XML file is correct for being parsed into Flash.

I still don't see the point of the parseXML (in bayes post) function considering that is making yet ANOTHER object in Flash just to make the array, when the XML object you load to is an array itself... it's basically duplicating an already existing object, but diving it up into chunks and putting those chunks in an array.

As stated in my previous post, when the XML information gets parsed into Flash, the nodes are all divided into arrays... which I guess in essence would be a multidimensional array. And you can target that array by targeting the childNodes of the parent node.... since childNodes is an array that holds all the nodes within the parent node tags.

Now typically I would set up XML prototypes to search through for what I want within the XML Object without having to create new arrays or objects and re-parse the XML data. But this is where coding is fun, everyone has their own methods :D mine is to make things as hard as possible on myself for no reason at all

dM: Passing data through tag attributes allows for a slimmer file and easier parsing. Why make the extra nodes if there's no reason for it :)
  • barrax
  • Newbie
  • Newbie
  • barrax
  • Posts: 12
  • Loc: Southampton, UK

Post 3+ Months Ago

To be honest i prefer not to pass through nodenames as data but seeing as bayes had a datasource that he couldn't change i just parsed the existing tree structure like this. If i havent got much text to pass through i like to keep the values as attributes - it allows for a slimmer tree structrue and you dont have to go tunneling through various branches.

I think my method of working with XML changes everytime i tackle a job. I only like to put them into arrays so that i can sort() (plus other array functions) them if need be. I'm not so sure you can do that while they are xml objects.

Well thats my story and im sticking with it;)

B
  • digitalMedia
  • a.k.a. dM
  • Genius
  • User avatar
  • Posts: 5149
  • Loc: SC-USA

Post 3+ Months Ago

Cool! Thanks guys.
  • UNFLUX
  • Genius
  • Genius
  • User avatar
  • Posts: 6376
  • Loc: twitter.com/unflux

Post 3+ Months Ago

good info guys, thanks. I learned something new today :)
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

barrax wrote:
To be honest i prefer not to pass through nodenames as data but seeing as bayes had a datasource that he couldn't change i just parsed the existing tree structure like this. If i havent got much text to pass through i like to keep the values as attributes - it allows for a slimmer tree structrue and you dont have to go tunneling through various branches.

I think my method of working with XML changes everytime i tackle a job. I only like to put them into arrays so that i can sort() (plus other array functions) them if need be. I'm not so sure you can do that while they are xml objects.

Well thats my story and im sticking with it;)

B


Yeah, I know what you mean about it changing for each project... since for every project you have a different stucture and the data may have to be parsed differently in Flash.

And that story is logical, so you can stick with it ;) 8)
  • barrax
  • Newbie
  • Newbie
  • barrax
  • Posts: 12
  • Loc: Southampton, UK

Post 3+ Months Ago

lol - cheers, but i didnt mean it like that.....

For example i dont usually parse xml into an array - i sometimes just access the xml objects themselves. It's only recently that i've started processing them into object arrays. oh well, each to their own etc etc.

nice forum btw - bookmarked! :)
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

I understood what you meant... im just retarded sometimes ;)
  • jpac
  • Newbie
  • Newbie
  • jpac
  • Posts: 5

Post 3+ Months Ago

Hi,
I saw this old post and i found it very useful. Is there a possibility to select specific attributes from all nodes?... for instance, from the xml code above, extract a list of all 'creatures' with one specific attribute.. let's say 'isa=mammal' ?

thanks in advance for any comment

jp
  • iamausten
  • Born
  • Born
  • iamausten
  • Posts: 1

Post 3+ Months Ago

mate i have this same problem...the odd thing is it works with 18 objects in the array....but not 19 - it's driving me nuts!
the code below works like a dream without the 19th & final (typical) image...

does anyone know a possible workaround?

xml code below:

XML Code: [ Select ]
<?xml version="1.0" encoding="utf-8"?>
<thumbnails>
   <thumbnail filename="thumb1.jpg" url="images/image1.jpg" target="_blank"
      title="Album One Title"
      description="" />
   <thumbnail filename="thumb2.jpg" url="images/image2.jpg" target="_blank"
      title="Album Two Title"
      description="" />
   <thumbnail filename="thumb3.jpg" url="images/image3.jpg" target="_blank"
      title="Album Three Title"
      description="" />
   <thumbnail filename="thumb4.jpg" url="images/image4.jpg" target="_blank"
      title="Album Four Title"
      description="" />
   <thumbnail filename="thumb5.jpg" url="images/image5.jpg" target="_blank"
      title="Album Five Title"
      description="" />
   <thumbnail filename="thumb6.jpg" url="images/image6.jpg" target="_blank"
      title="Album Six Title"
      description="" />
   <thumbnail filename="thumb7.jpg" url="images/image7.jpg" target="_blank"
      title="Album Seven Title"
      description="" />
   <thumbnail filename="thumb8.jpg" url="images/image8.jpg" target="_blank"
      title="Album Eight Title"
      description="" />
   <thumbnail filename="thumb9.jpg" url="images/image9.jpg" target="_blank"
      title="Album Nine Title"
      description="" />
   <thumbnail filename="thumb10.jpg" url="images/image10.jpg" target="_blank"
      title="Album Ten Title"
      description="" />
   <thumbnail filename="thumb11.jpg" url="images/image11.jpg" target="_blank"
      title="Album Ten Title"
      description="" />
   <thumbnail filename="thumb12.jpg" url="images/image12.jpg" target="_blank"
      title="Album Ten Title"
      description="" />
   <thumbnail filename="thumb13.jpg" url="images/image13.jpg" target="_blank"
      title="Album Ten Title"
      description="" />
   <thumbnail filename="thumb14.jpg" url="images/image14.jpg" target="_blank"
      title="Album Ten Title"
      description="" />
   <thumbnail filename="thumb15.jpg" url="images/image15.jpg" target="_blank"
      title="Album Ten Title"
      description="" />
   <thumbnail filename="thumb16.jpg" url="images/image16.jpg" target="_blank"
      title="Album Ten Title"
      description="" />
   <thumbnail filename="thumb17.jpg" url="images/image17.jpg" target="_blank"
      title="Album Ten Title"
      description="" />
   <thumbnail filename="thumb18.jpg" url="images/image18.jpg" target="_blank"
      title="Album Ten Title"
      description="" />
   <thumbnail filename="thumb19.jpg" url="images/image19.jpg" target="_blank"
      title="Album Ten Title"
      description="" />
</thumbnails>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <thumbnails>
  3.    <thumbnail filename="thumb1.jpg" url="images/image1.jpg" target="_blank"
  4.       title="Album One Title"
  5.       description="" />
  6.    <thumbnail filename="thumb2.jpg" url="images/image2.jpg" target="_blank"
  7.       title="Album Two Title"
  8.       description="" />
  9.    <thumbnail filename="thumb3.jpg" url="images/image3.jpg" target="_blank"
  10.       title="Album Three Title"
  11.       description="" />
  12.    <thumbnail filename="thumb4.jpg" url="images/image4.jpg" target="_blank"
  13.       title="Album Four Title"
  14.       description="" />
  15.    <thumbnail filename="thumb5.jpg" url="images/image5.jpg" target="_blank"
  16.       title="Album Five Title"
  17.       description="" />
  18.    <thumbnail filename="thumb6.jpg" url="images/image6.jpg" target="_blank"
  19.       title="Album Six Title"
  20.       description="" />
  21.    <thumbnail filename="thumb7.jpg" url="images/image7.jpg" target="_blank"
  22.       title="Album Seven Title"
  23.       description="" />
  24.    <thumbnail filename="thumb8.jpg" url="images/image8.jpg" target="_blank"
  25.       title="Album Eight Title"
  26.       description="" />
  27.    <thumbnail filename="thumb9.jpg" url="images/image9.jpg" target="_blank"
  28.       title="Album Nine Title"
  29.       description="" />
  30.    <thumbnail filename="thumb10.jpg" url="images/image10.jpg" target="_blank"
  31.       title="Album Ten Title"
  32.       description="" />
  33.    <thumbnail filename="thumb11.jpg" url="images/image11.jpg" target="_blank"
  34.       title="Album Ten Title"
  35.       description="" />
  36.    <thumbnail filename="thumb12.jpg" url="images/image12.jpg" target="_blank"
  37.       title="Album Ten Title"
  38.       description="" />
  39.    <thumbnail filename="thumb13.jpg" url="images/image13.jpg" target="_blank"
  40.       title="Album Ten Title"
  41.       description="" />
  42.    <thumbnail filename="thumb14.jpg" url="images/image14.jpg" target="_blank"
  43.       title="Album Ten Title"
  44.       description="" />
  45.    <thumbnail filename="thumb15.jpg" url="images/image15.jpg" target="_blank"
  46.       title="Album Ten Title"
  47.       description="" />
  48.    <thumbnail filename="thumb16.jpg" url="images/image16.jpg" target="_blank"
  49.       title="Album Ten Title"
  50.       description="" />
  51.    <thumbnail filename="thumb17.jpg" url="images/image17.jpg" target="_blank"
  52.       title="Album Ten Title"
  53.       description="" />
  54.    <thumbnail filename="thumb18.jpg" url="images/image18.jpg" target="_blank"
  55.       title="Album Ten Title"
  56.       description="" />
  57.    <thumbnail filename="thumb19.jpg" url="images/image19.jpg" target="_blank"
  58.       title="Album Ten Title"
  59.       description="" />
  60. </thumbnails>
Moderator Remark: added code tags
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

iamausten wrote:
mate i have this same problem...the odd thing is it works with 18 objects in the array....but not 19 - it's driving me nuts!
the code below works like a dream without the 19th & final (typical) image...

does anyone know a possible workaround?


Post your actionscript code. I highly doubt its a problem with the xml.

Post Information

  • Total Posts in this topic: 23 posts
  • Users browsing this forum: No registered users and 11 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
 
cron
 

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