6 Replies Latest reply on Jun 7, 2007 10:24 AM by hciguy

    XML node has one child?

    hciguy Level 1
      Hey guys,

      I have a recursive function that search through an XML document and finds each node, whether it has children, etc. When it comes across a block like the following:

      <mainitem submenu="true" name="File">
      <item submenu="false">Option 1</item>
      <item submenu="false">Option 2</item>
      <item submenu="false">Option 3</item>
      <item submenu="false">Option 4or</item>
      <item submenu="false">Option 5</item>
      </mainitem>

      It correctly states that the first node, "File," has 5 children. But when it recurses to the child nodes it states that each node has 1 child. I am using children().length() to check for number of children....below is my function:

      private function parseMenuData(menuData:XML):void{

      for each(var menuItem:XML in menuData.elements()){

      trace("-----------------------------------------------------------");
      trace("Menuitem " + menuItem.@name + "has " + menuItem.children().length() + " children.");

      if(menuItem.children().length() > 0){
      parseMenuData(menuItem);
      }else{
      trace("No children here");
      }

      //parseMenuChildren(menuItem);

      }
      }

      Thanks in advance!
        • 1. Re: XML node has one child?
          hciguy Level 1
          Actually...it looks like <node>Text</node> has one child because it has "Text" in there....while <node name="text" /> has no children. Are the contents of a node considered a child?

          Thanks.
          • 2. Re: XML node has one child?
            Level 7
            hciguy wrote:
            > Are the contents of a node considered a child?


            Yes, it's value can best be retrieved with:

            childNodes[n].nodeValue

            place this in a keyframe and test:
            var xml:XML = new XML("<test>content</test>");
            trace(xml.childNodes.length);
            trace(xml.childNodes[0]); // points to the "root"
            trace(xml.childNodes[0].childNodes[0]); // first childnode of "root"
            trace(xml.childNodes[0].childNodes[0].nodeValue); // it's nodevalue

            it should render:
            1
            <test>content</test>
            content
            content

            ofcourse childNodes[0] can be replaced with: firstChild (maybe even a
            few nanoseconds faster)

            HTH,
            Manno

            --
            ----------
            Manno Bult
            http://www.aloft.nl
            • 3. Re: XML node has one child?
              hciguy Level 1
              When I use xml.childNodes.length I get nothing, as opposed to getting the appropriate number of children when I use children().length() - any ideas why?

              Thanks for the help.
              • 4. Re: XML node has one child?
                Level 7
                hciguy wrote:
                > When I use xml.childNodes.length I get nothing, as opposed to getting the appropriate number of children when I use children().length() - any ideas why?
                >
                > Thanks for the help.

                Oh buggers, my CS3 package is due tomorrow, are you on AS3? My example
                should work in AS2.


                --
                ----------
                Manno Bult
                http://www.aloft.nl
                • 5. Re: XML node has one child?
                  hciguy Level 1
                  Yes, AS3
                  • 6. Re: XML node has one child?
                    hciguy Level 1
                    Manno,

                    Do you happen to know the logic I need to use to go through an entire XML structure? So if I have an XML structure with multiple levels, how do I go through it? Right now I am using a recursive function which gives me each node just fine but I can't quite figure out how to designate what level I am at in the structure as well as how to load the entire structure into a mutli-dimensional array. Any ideas?

                    Thanks.