12 Replies Latest reply on Apr 19, 2010 8:26 PM by macneil2222

    Repeat XML child nodes

    macneil2222

      Hello,

       

      Can anyone tell me how to repeat child nodes without having to call them separately in AS.  I've been trying to understand this for a while.

       

      This is my info.xml

       

      <?xml version="1.0"?>
      <news>
      <item>
      <headline>News Headline 1</headline>
      <article>Article Text 1</article>
      </item>
      <item>
      <headline>News Headline 2</headline>
      <article>Article Text 2</article>
      </item>
      <item>
      <headline>News Headline 3</headline>
      <article>Article Text 3</article>
      </item>
      </news>

       

      This is my AS for 2 txt fields, headline_txt and article_txt

       

      xmlFile = new XML();
      xmlFile.ignoreWhite = true;
      xmlFile.onLoad = loadXMLData;
      xmlFile.load("info.xml");

      function loadXMLData(loaded) {
      if (loaded) {
      headline_txt.text = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
      article_txt.text = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue; 
      }
      else {
      trace("Could not load XML file");
      }
      }

       

      It returns the first item ok but I was trying to loop each item automatically and I thought it was with something like

       

      item = item.nextSibling;

       

      Thanks

        • 1. Re: Repeat XML child nodes
          Harry Kunz Level 4

          You can get the number of nodes and loop like this:

           

          xmlFile = new XML();

          xmlFile.ignoreWhite = true;

          xmlFile.onLoad = loadXMLData;

          xmlFile.load("info.xml");

          function loadXMLData(loaded) {

          if (loaded) {

          var nNodes:Number = xmlFile.firstChild.childNodes.length;

          for (var i:Number = 0; i<nNodes; i++) {

          var cNode:XMLNode = this.firstChild.childNodes[i];

          trace("Headline"+i+": "+cNode.childNodes[0].firstChild.nodeValue);

          trace("Article"+i+": "+cNode.childNodes[1].firstChild.nodeValue);

          //headline_txt.text = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;

          //article_txt.text = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue; 

          }

          } else {

          trace("Could not load XML file");

          }

          }

          1 person found this helpful
          • 2. Re: Repeat XML child nodes
            macneil2222 Level 1

            Hi thanks for the reply... that's great thanks, it's putting what i need in the output box.

             

            I'm just picking this up, can you tell me how to get that output into a dynamic field or both original fields?

             

            Thanks again

            • 3. Re: Repeat XML child nodes
              Harry Kunz Level 4

              I modified the code to use Delegate class since you will have a scope issue with the previous implementation. Here make sure you have your text fields on stage have appended numbers zero to two (headline_txt0, headline_txt1, headline_txt2, article_txt0, article_txt1, article_txt2). Then use this code:

               

              import mx.utils.Delegate;

              xmlFile = new XML();

              xmlFile.ignoreWhite = true;

              xmlFile.onLoad = Delegate.create(this, loadXMLData);

              xmlFile.load("info.xml");

              function loadXMLData(loaded) {

              if (loaded) {

              var nNodes:Number = xmlFile.firstChild.childNodes.length;

              for (var i:Number = 0; i<nNodes; i++) {

              var cNode:XMLNode = xmlFile.firstChild.childNodes[i];

              this["headline_txt"+i].text = cNode.childNodes[0].firstChild.nodeValue;

              this["article_txt"+i].text = cNode.childNodes[1].firstChild.nodeValue;

              }

              } else {

              trace("Could not load XML file");

              }

              }

              1 person found this helpful
              • 4. Re: Repeat XML child nodes
                macneil2222 Level 1

                There are only 2 txt boxes and I appended both to headline_txt0 and article_txt0, no results came back sorry...  Would this method not mean I need a txt box for node of the schema?

                 

                I was looking at something along the lines of

                 

                while (item != null)

                 

                item = item.nextSibling;

                 

                otherwise anything to get it to output to the stage...

                 

                Thanks again

                • 5. Re: Repeat XML child nodes
                  macneil2222 Level 1

                  This done what I needed but was in AS1 and with different xml.  So I'm trying to copy the loop routing really...

                   

                   

                  // The first step is to activate the XML object
                  headlineXML = new XML();
                  /*
                  With the XML Object now active you must now load an XML foramtted document.
                  Any DTD or XLS formatting will be ignored.
                  */
                  headlineXML.onLoad = myLoad;
                  headlineXML.load("headlines.xml");
                  // Before proceeding to far into the program, make sure the XML document has loaded
                  // Extract information from the XML file
                  function myLoad(ok) {
                  if (ok == true) {
                    Publish(this.firstChild);
                  }
                  }

                  function Publish(HeadlineXMLNode) {
                  if (HeadlineXMLNode.nodeName.toUpperCase() == "BROADCAST") {
                    content = "";
                    story = HeadlineXMLNode.firstChild;
                    while (story != null) {
                     if (story.nodeName.toUpperCase() == "STORY") {
                      lead = "";
                      body = "";
                      URL = "";
                      element = story.firstChild;
                      while (element != null) {
                       if (element.nodeName.toUpperCase() == "LEAD") {
                        lead = element.firstChild.nodeValue;
                       }
                       if (element.nodeName.toUpperCase() == "BODY") {
                        body = element.firstChild.nodeValue;
                       }
                       if (element.nodeName.toUpperCase() == "URL") {
                        URL = element.firstChild.nodeValue;
                       }
                       element = element.nextSibling;
                      }
                      content += "<font size='+2' color='#3366cc'><a href='"+URL+"'>"+lead+"</a></font><br>"+body+"<br><br>";
                      txt.htmltext=content;
                     }
                     story = story.nextSibling;
                    }
                  }
                  }

                  • 6. Re: Repeat XML child nodes
                    Harry Kunz Level 4

                    Well you can use nextSibling but it doesn't make any difference. So you only have 2 textboxes headline_txt0 and article_txt0 where you want to append all the text in, is that correct?

                     

                    import mx.utils.Delegate;

                    headline_txt0.text = "";

                    article_txt0.text = "";

                    xmlFile = new XML();

                    xmlFile.ignoreWhite = true;

                    xmlFile.onLoad = Delegate.create(this, loadXMLData);

                    xmlFile.load("info.xml");

                    function loadXMLData(loaded) {

                    if (loaded) {

                    var nNode:Number = 0;

                            var cNode:XMLNode = xmlFile.firstChild.childNodes[0];

                     

                    while(cNode != null) {

                    trace(cNode)

                    headline_txt0.text += cNode.childNodes[0].firstChild.nodeValue;

                    article_txt0.text += cNode.childNodes[1].firstChild.nodeValue;

                    cNode = cNode.nextSibling;

                    }

                    } else {

                    trace("Could not load XML file");

                    }

                    }

                     

                    I'm not sure if i understand what you are trying to do. Does this code not work for you? What do you mean by "node of the schema"

                    • 7. Re: Repeat XML child nodes
                      macneil2222 Level 1

                      Thanks for the help... Nope it's not picking up anything with the code.  I tried it with different instance names too in the txt fields.  I'm trying to get:

                       

                      <?xml version="1.0"?>
                      <news>
                      <item>
                      <headline>News Headline 1</headline>
                      <article>Article Text 1</article>
                      </item>
                      <item>
                      <headline>News Headline 2</headline>
                      <article>Article Text 2</article>
                      </item>
                      <item>
                      <headline>News Headline 3</headline>
                      <article>Article Text 3</article>
                      </item>
                      </news>

                       

                      To appear on the stage like:

                       

                      News Headline 1

                      Article Text 1

                       

                      News Headline 2

                      Article Text 2

                       

                      News Headline 3

                      Article Text 3

                      • 8. Re: Repeat XML child nodes
                        Harry Kunz Level 4

                        What value is placed in your text box after code execution? Make sure your instance names match the ones in the code. If you are trying to display your text in that fashion, why not use 6 text fields and use the code i posted in my 2nd reply?

                        • 9. Re: Repeat XML child nodes
                          macneil2222 Level 1

                          I appreciate the help...  Instance names are the same, but there is nothing going into either of the two fields.  I know it would work with 6 boxes but I was wanting to code it and leave it up to the loop and XML to do the rest like the AS1 example.

                           

                          I'll keep having a look at it, it's all nearly there...

                           

                          Thanks again

                          • 10. Re: Repeat XML child nodes
                            Harry Kunz Level 4

                            Ahh okay. You can create text fields at runtime so that it is dynamically placed on stage depending on how many headers/articles you have. Try this:

                             

                            import mx.utils.Delegate;

                            var nX:Number = 10;

                            var nY:Number = 10;

                            var nH:Number = 30;

                            var nW:Number = 200;

                            var txtFmt:TextFormat = new TextFormat();

                            txtFmt.color = 0x0000FF;

                            txtFmt.size = 20;

                            xmlFile = new XML();

                            xmlFile.ignoreWhite = true;

                            xmlFile.onLoad = Delegate.create(this, loadXMLData);

                            xmlFile.load("info.xml");

                            function loadXMLData(loaded) {

                            if (loaded) {

                            var nNode:Number = 0;

                            var cNode:XMLNode = xmlFile.firstChild.childNodes[0];

                             

                            while(cNode != null) {

                            var n:Number = this.getNextHighestDepth();

                            var txtHeader:TextField = this.createTextField("h"+n,n,nX,nY,nW,nH);

                            var txtArticle:TextField = this.createTextField("a"+n,n+1,nX,nY+20,nW,nH);

                            txtHeader.text += cNode.childNodes[0].firstChild.nodeValue;

                            txtArticle.text += cNode.childNodes[1].firstChild.nodeValue;

                            cNode = cNode.nextSibling;

                            txtHeader.setTextFormat(txtFmt);

                            txtArticle.setTextFormat(txtFmt);

                            nY += nH + 20;

                            }

                            } else {

                            trace("Could not load XML file");

                            }

                            }

                             

                            No more need for text fields on stage. They will be created dynamically at runtime.

                            • 11. Re: Repeat XML child nodes
                              macneil2222 Level 1

                              Your second example shows this in the output box of flash after ctrl enter:

                               

                              Headline0: News Headline 1
                              Article0: Article Text 1
                              Headline1: News Headline 2
                              Article1: Article Text 2
                              Headline2: News Headline 3
                              Article2: Article Text 3

                               

                              Not in the headline or article fields unfortunately...

                              • 12. Re: Repeat XML child nodes
                                macneil2222 Level 1

                                Thanks to Harry for the help but this has my brain fried.  I have been trying different combinations all day and I still can't get a loop to work.  I can get it working in AS1 and AS3...  I think I've been through every tutorial on xml and as2 trying to get a loop into my script...  I have tried it as an Array also.  Looking on bright side I've learnt enough to have my text content all XML even if it doesn't loop news item nodes.

                                 

                                Anyone?