Problem reading XML file
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Problem reading XML file

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    17

    Problem reading XML file

    Hi, Im trying to parse an XML file and I have a problem to read the childs of a node.
    My XML file have this structure:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <code>
       <images jpgURL="small/m1.jpg" url="big/bg1.jpg" name="Lorem Ipsum">
          <buttons>
             <button posX="550" posY="438" textX="Lorem Ipsum1"/>
             <button posX="620" posY="438" textX="Lorem Ipsum2"/>
             <button posX="650" posY="116" textX="Lorem Ipsum3"/>
             <button posX="120" posY="248" textX="Lorem Ipsum4"/>
          </buttons>
       </images >
       <images jpgURL="small/m2.jpg" url="big/bg2.jpg" name="Lorem Ipsum">
          <buttons>
             <button posX="240" posY="268" textX="Lorem Ipsum1"/>
             <button posX="435" posY="388" textX="Lorem Ipsum2"/>
          </buttons>
       </images >
    </code>
    And here is my AS code:

    Code:
    function initXml(miXML){
       var totals = miXML.firstChild.childNodes;
        
       for(var i=0; i < totals.length; i++){
          var thumbs = totals[i];
          var buttons = thumbs.firstChild.childNodes;
          
          var thumb = thumbLoader.attachMovie("item1_mc", "item1" + i, i + 1000, {id: i, num: i});
          
          for (var j=0; j < buttons .length; j++){
                var button = buttons [j];
                trace("posX= "+ boton.attributes.posX +" posY= "+ boton.attributes.posY +" text= " + boton.attributes.texto);
          }
       } 
    }
    The Output panel shows this traces:
    posX= 550 posY= 438 text= Lorem Ipsum1
    posX= 240 posY= 438 text= Lorem Ipsum1
    And I want that only trace all the childNodes of the first button node, like this:

    posX= 550 posY= 438 text= Lorem Ipsum1
    posX= 240 posY= 438 text= Lorem Ipsum2
    posX= 650 posY= 116 text= Lorem Ipsum3
    posX= 120 posY= 248 text= Lorem Ipsum4
    How can I trace this?

    Thank you!

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,514
    Code:
    var xml = new XML('<code><images jpgURL="small/m1.jpg" url="big/bg1.jpg" name="Lorem Ipsum"><buttons><button posX="550" posY="438" textX="Lorem Ipsum1"/><button posX="620" posY="438" textX="Lorem Ipsum2"/><button posX="650" posY="116" textX="Lorem Ipsum3"/><button posX="120" posY="248" textX="Lorem Ipsum4"/></buttons></images><images jpgURL="small/m2.jpg" url="big/bg2.jpg" name="Lorem Ipsum"><buttons><button posX="240" posY="268" textX="Lorem Ipsum1"/><button posX="435" posY="388" textX="Lorem Ipsum2"/></buttons></images></code>');
    
    function initXml(miXML) {
            var totals = miXML.firstChild.childNodes;
    
            //for (var i = 0; i < totals.length; i++) {
                    var thumbs = totals[0];
                    var buttons = thumbs.firstChild.childNodes;
    
                    //var thumb = thumbLoader.attachMovie("item1_mc", "item1" + i, i + 1000, {id:i, num:i});
    
                    for (var j = 0; j < buttons.length; j++) {
                            var button = buttons[j];
                            trace("posX= " + button.attributes.posX + " posY= " + button.attributes.posY + " text= " + button.attributes.textX);
                    }
            //}
    }
    initXml(xml);

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    17
    Thank you very much dawsonk!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com