XML parsing - total newbid - can you help?

  • panzhuli
  • Graduate
  • Graduate
  • panzhuli
  • Posts: 135
  • Loc: Cleveland

Post 3+ Months Ago

Hey all -

This is my first project parsing xml. Can someone direct me with very basic help? here's the xml doc:

Code: [ Select ]
<?xml version="1.0" encoding="utf-8"?>
<data>
 
    <menu>
        <item id="1" um="GAMEINFO" label="THE GAME"/>
        <item id="2" um="COMMUNITY_NEWS" label="NEWS"/>
        <item id="3" um="MEDIA_THEATRICAL" label="THE TIMELINE"/>
        <item id="4" um="DOWNLOAD" label="MEDIA"/>
        <item id="5" label="FORUMS"/>
        <item id="6" label="BUY"/>
        <item id="7" label="REGISTER"/>
    </menu>
   
    <promos>
        <promo img="promo_1.jpg" />
        <promo img="promo_2.jpg" />
        <promo img="promo_3.jpg" />
    </promos>
   
    <news title="Game News" />
   
    <media>
        <category label="Videos" type="video" />
        <category label="Screenshots" type="screenshot" />
        <category label="Downloads" type="download" />
    </media>
   
</data>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <data>
  3.  
  4.     <menu>
  5.         <item id="1" um="GAMEINFO" label="THE GAME"/>
  6.         <item id="2" um="COMMUNITY_NEWS" label="NEWS"/>
  7.         <item id="3" um="MEDIA_THEATRICAL" label="THE TIMELINE"/>
  8.         <item id="4" um="DOWNLOAD" label="MEDIA"/>
  9.         <item id="5" label="FORUMS"/>
  10.         <item id="6" label="BUY"/>
  11.         <item id="7" label="REGISTER"/>
  12.     </menu>
  13.    
  14.     <promos>
  15.         <promo img="promo_1.jpg" />
  16.         <promo img="promo_2.jpg" />
  17.         <promo img="promo_3.jpg" />
  18.     </promos>
  19.    
  20.     <news title="Game News" />
  21.    
  22.     <media>
  23.         <category label="Videos" type="video" />
  24.         <category label="Screenshots" type="screenshot" />
  25.         <category label="Downloads" type="download" />
  26.     </media>
  27.    
  28. </data>


I want to label my 6 buttons with the labels from the <menu> child. I'm getting just blank space. here's what I have so far:

Code: [ Select ]
 
var document_xml = new XML();
document_xml.onLoad = loaded;
function loaded(success){
    trace ("loaded");
    gotoAndStop("loaded");
}
document_xml.load("xml_file.xml");
 
  1.  
  2. var document_xml = new XML();
  3. document_xml.onLoad = loaded;
  4. function loaded(success){
  5.     trace ("loaded");
  6.     gotoAndStop("loaded");
  7. }
  8. document_xml.load("xml_file.xml");
  9.  

this loads and progresses...

and on each of the buttons:

Code: [ Select ]
 
trace(_root.document_xml.childNodes[0].childNodes[0].nodeValue);
 
  1.  
  2. trace(_root.document_xml.childNodes[0].childNodes[0].nodeValue);
  3.  


what i will do, is take the name of the button (1-6) and then parse the xml, but I can't seem to get to the values. Can someone help? I just want to get the value of the first one and then i'll be able to do the rest!!!

Thanks!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

well first of all you don't actually have any node values...

Node values look like this in xml
Code: [ Select ]
<nodeName>Node Value Text</nodeName>
You actually have node attributes which is anything inside the node tag specified with an =""

So the proper AS to get an attribute would be
Code: [ Select ]
_root.document_xml.childNodes[0].childNodes[0].attributes.label
Thats assuming your actually getting tot he correct node through your crazy hierarchy.

I typically find that its your best bet to split that huge xml array out into smaller more manageable sections IE
Code: [ Select ]
var menucontent:Array = _root.document_xml.firstChild.childNodes[0];
That would give you just the menu section of your xml as an array which is much easier to deal with.

Kirupa has a great Actionscript XML tutorial that might help you quite a bit.
  • panzhuli
  • Graduate
  • Graduate
  • panzhuli
  • Posts: 135
  • Loc: Cleveland

Post 3+ Months Ago

thanks - i'll check that out - you can see I don't know what I'm doing!
  • panzhuli
  • Graduate
  • Graduate
  • panzhuli
  • Posts: 135
  • Loc: Cleveland

Post 3+ Months Ago

hey all -

so I'm now running into a problem actually loading the file. I've tried this code (which works)

Code: [ Select ]
var document_xml = new XML("xml code pasted here");
trace (document_xml.firstChild.firstChild.childNodes[0].attributes.label);
  1. var document_xml = new XML("xml code pasted here");
  2. trace (document_xml.firstChild.firstChild.childNodes[0].attributes.label);


this shows me exactly what I'd expected.... but this... gives me an undefined:

Code: [ Select ]
var document_xml = new XML();
document_xml.onLoad = loaded;
function loaded(success){
    trace ("loaded");
    trace (document_xml.firstChild.firstChild.childNodes[0].attributes.label);
}
document_xml.load("Data.xml");
  1. var document_xml = new XML();
  2. document_xml.onLoad = loaded;
  3. function loaded(success){
  4.     trace ("loaded");
  5.     trace (document_xml.firstChild.firstChild.childNodes[0].attributes.label);
  6. }
  7. document_xml.load("Data.xml");


same directory. the xml loads (per the "loaded" trace command). Ideas? Thanks!
  • panzhuli
  • Graduate
  • Graduate
  • panzhuli
  • Posts: 135
  • Loc: Cleveland

Post 3+ Months Ago

got it -

document_xml.ignoreWhite = true;
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

glad it worked. funny how the little things make such a difference huh?

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 22 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.