4 Replies Latest reply on Apr 7, 2009 12:27 AM by Richard_Abbott

    XML Help

    released87
      Hi,

      I have an XML file with the following structure:

      <products>
      <product>//Repeating Node
      <id></id>
      <category></category>
      <subcategory></subcategory>
      ....more nodes....
      </product
      </products>

      As you can see, I basically have a product list, which has both category and subcategory elements as siblings. However, I need to build a new XML Object using the category element as the repeating, unique element, and have all subcategories as the child of the category.

      Example:
      <category name="colors">
      <subcategory>blue</subcategory>
      <subcategory>red</subcategory>
      </category>
      <category name="foods">
      <subcategory>Pizza</subcategory>
      <subcategory>Buffalo Wings</subcategory>
      </category>

      This is what I have now. The category nodes work fine and are unique, however I am unable to get the subcategories,

        • 1. Re: XML Help
          funkysoul Level 4

          well you can't have 2 equally named elements inside of a node.

          You need to restructure your XML to fit your needs without using the same element name twice in a single node.

           

          Maybe you could do something like this:

           

          <category name="colors">
          <subcategory>blue</subcategory>
          <subcategory1>red</subcategory1>
          </category>
          <category name="foods">
          <subcategory>Pizza</subcategory>
          <subcategory1>Buffalo Wings</subcategory1>
          </category>

          • 2. Re: XML Help
            kid electric

            Tiago &quot;funkysoul&quot; Dias wrote:

             

            well you can't have 2 equally named elements inside of a node.


            Really?  I'm not an XML expert, but I think that's not true.  Otherwise, how can you have

             

            <products>

                 <product> Product 1 </product>

                 <product> Product 2 </product>

            </products>

             

            ??

             

            Of course you can have multiple elements with the same name!  That's sort of the point of XML.  Then you can do a "for each... in" operation to find every node of the same tag/element label.

             

            Unless I'm confused about something here... (like I said, I'm not an expert, but I'm pretty sure you can nest multiple elements of the same type)

            • 3. Re: XML Help
              funkysoul Level 4

              Now you confusing me as well

               

              When you using attributes you can use the same element name so you can loop through them for example:

               

              <?xml version="1.0"?>
              <menu name="links">
                  <menu name="macromedia">
                      <item name="flash" action="gotoURL" variables="http://www.macromedia.com/software/flash"/>
                      <item name="dreamweaver" action="gotoURL" variables="http://www.macromedia.com/software/dreamweaver"/>
                      <item name="fireworks" action="gotoURL" variables="http://www.macromedia.com/software/fireworks"/>
                  </menu>
                  <menu name="kirupa">
                      <item name="home" action="gotoURL" variables="http://www.kirupa.com"/>
                      <item name="forums" action="gotoURL" variables="http://www.kirupaforum.com/forums/index.php"/>
                      <menu name="tutorials">
                          <item name="actionscript" action="gotoURL" variables="http://www.kirupa.com/developer/actionscript/index.htm"/>
                          <item name="photoshop" action="gotoURL" variables="http://www.kirupa.com/photoshop/index.htm"/>
                          <item name="web" action="gotoURL" variables="http://www.kirupa.com/web/index.htm"/>
                      </menu>
                  </menu>
                  <item name="google" action="gotoURL" variables="http://www.google.com"/>
              </menu>

               

              Above you have the same element name but the attribute name is different.

               

              I must admit now that I'm more awake then dead, you could use the same element name but that would be a pain to read that xml in when targeting specific nodes and it's not that quite readable anymore.

               

              Apart from that you normally and I mean NORMALLY loop through the nodes, not through the children of the node.

              • 4. Re: XML Help
                Richard_Abbott Level 3

                Tiago,

                your XML looks like...

                xml_var =

                <root>
                <category name="colors">
                <subcategory>blue</subcategory>
                <subcategory>red</subcategory>
                </category>
                <category name="foods">
                <subcategory>Pizza</subcategory>
                <subcategory>Buffalo Wings</subcategory>
                </category>

                </root>

                To get out all subcategories regardless of category you can do

                xml_var.descendants("subcategory");

                which will give you an XMLList with all subcategories regardless of immedaite parent.

                To get out just the colours (sorry, colors!) subcategories you could use

                xml_var.category.(@name=="colors").children();

                which (presupposing I have got the dots in the right place) will give you just the blue and red entries.

                Hope that helps,

                Richard