11 Replies Latest reply on Dec 26, 2008 12:17 PM by knolcollector

    Search "For Each" XML Flex 3.0 not working

    knolcollector
      This returns all the xml elements, not just the single element I want. Any ideas?
        • 1. Re: Search "For Each" XML Flex 3.0 not working
          rtalton Level 4
          I couldn't figure out what was happening, so I tried this and it worked.

          • 2. Re: Search "For Each" XML Flex 3.0 not working
            knolcollector Level 1
            Thanks so much rtalton for taking the time to reply, I haven't tried your code out yet, but it looks good.

            Two questions:

            (1) With your method, you will have to know schema of the xml ahead of time? If so the method is less reusable.
            (2) You code seems to search the xml for any element with the id="001", which is fairly unique, but this isn't as targeted as being able to search specific children.
            • 3. Re: Search "For Each" XML Flex 3.0 not working
              rtalton Level 4
              1) Yes, you would have to know the schema ahead of time.
              2) I used your example of a search or filter based on an id number of '001', which is an attribute in your sample XML file. The same technique can be adapted to filter on any element or attribute using e4x syntax. The same syntax can also be used on the dataProvider of a List-based component to filter the view without writing any code.

              A couple of notes...
              Not knowing the XML schema of the data you are retrieving is not unheard of, but really would be rare, I think. We should know the data before building an application to retrieve it, manipulate it and display it. I can't think of a practical application otherwise.

              I had difficulty understanding the intent of your code, so I just guessed you were trying to display details about images and their photographers, locations, etc. I put together an example showing one way of:
              1) retrieving your XML data using HTTPService and specifying the e4x format,
              2) assigning the results to an XMLListCollection,
              3) using the HTTPService result to manually parse the results with a for...each statement and formatting them with HTML tags for display in a text area. If the XML were set up differently, I could have assigned all image details to a simple List, probably without writing any code.

              These are all things I had to learn the hard way, so I hope it's useful!
              • 4. Re: Search "For Each" XML Flex 3.0 not working
                knolcollector Level 1
                It's really useful & I appreciate it.

                I was working through a lynda.com tutorial on using XML and AS3.0. The tutorial was working out of Flash CS3, but as Flex uses AS3.0 as well I was translating the code over. This may be where the issues arises, but I doubt it.

                In terms of the intent of the code, the tutorial was to show how loop through XML and search for certain elements.



                • 5. Re: Search "For Each" XML Flex 3.0 not working
                  rtalton Level 4
                  XML is tougher to handle as it is DOM-based; I'd recommend using e4x with your data as its methods are far easier (for me) to implement-you can access child nodes by name as properties of parent nodes.

                  The two approaches do not share the same methods; they are very different. here's an excellent link showing what you can do using e4x syntax:
                  http://developer.yahoo.com/flash/articles/e4x-beginner-to-advanced.html


                  • 6. Re: Search "For Each" XML Flex 3.0 not working
                    knolcollector Level 1
                    The Lynda tutorial and my code uses E4X methods. Thanks for the link, it looks really good.
                    • 7. Re: Search "For Each" XML Flex 3.0 not working
                      Level 7

                      "knolcollector" <webforumsuser@macromedia.com> wrote in message
                      news:giucdt$9um$1@forums.macromedia.com...
                      > Thanks so much rtalton for taking the time to reply, I haven't tried your
                      > code
                      > out yet, but it looks good.
                      >
                      > Two questions:
                      >
                      > (1) With your method, you will have to know schema of the xml ahead of
                      > time?
                      > If so the method is less reusable.
                      > (2) You code seems to search the xml for any element with the id="001",
                      > which
                      > is fairly unique, but this isn't as targeted as being able to search
                      > specific
                      > children.

                      Could you be more clear about exactly what search result you're seeking?


                      • 8. Re: Search &quot;For Each&quot; XML Flex 3.0 not working
                        knolcollector Level 1
                        I want the ability to search a specific element within the xml.

                        e.g. Search all the <title> elements for a specific string. Then return that element and all the children in it's parents.

                        If it's children have another "element" (not sure of the name) so say:

                        <image>
                        <title>Example</title>
                        <descrp>This is cool></descrp>
                        <photodude>
                        <name>Jack</name>
                        <location>LA</location>
                        </photodude>
                        </image>

                        Grab image and then also photodude details.

                        I was just working thru a lynda tutorial, so I'm just trying to understand how your code matches up against the lynda.com code.
                        • 9. Re: Search &quot;For Each&quot; XML Flex 3.0 not working
                          Level 7

                          "knolcollector" <webforumsuser@macromedia.com> wrote in message
                          news:gj18hh$qkr$1@forums.macromedia.com...
                          >I want the ability to search a specific element within the xml.
                          >
                          > e.g. Search all the <title> elements for a specific string. Then return
                          > that
                          > element and all the children in it's parents.
                          >
                          > If it's children have another "element" (not sure of the name) so say:
                          >
                          > <image>
                          > <title>Example</title>
                          > <descrp>This is cool></descrp>
                          > <photodude>
                          > <name>Jack</name>
                          > <location>LA</location>
                          > </photodude>
                          > </image>
                          >
                          > Grab image and then also photodude details.
                          >
                          > I was just working thru a lynda tutorial, so I'm just trying to understand
                          > how
                          > your code matches up against the lynda.com code.

                          I think the problem is that in your code, you're searching for the id "001",
                          then when you get it's parent, you get everything. So why bother searching
                          for the id "001" in the first place? This is what I'm not clear on. So in
                          the example above, if you search for the image with the title "Example",
                          then get all of the children of its parent, then you'll get all images.

                          This seems to be what your existing code did and it's what you say you want,
                          so where's your problem?


                          • 10. Search &quot;For Each&quot; XML Flex 3.0 not working
                            rtalton Level 4
                            It may be helpful to clearly identify the XML terminology ( attribute, node...) before you decide how to search your xml data. You indicated you wanted to search both. You can certainly try to process xml without knowing its structure beforehand, but this would be tedious and inefficient. Searching through XML attributes and nodes take two different approaches. You'll end up with lots of inefficient looping.

                            The code you posted, and the example you gave, are already making assumptions about the structure of the xml data, i.e. "id" attribute, "title" node. So you are not flying blind. Since you already know you want to, for instance, search all nodes named "title" for a particular string, you can simply filter the data like this:
                            var e4xList:XMLList = xml.*.(title.toString() == strElementValue);
                            ...and you're done. There's no need to loop through each node.
                            • 11. Re: Search &quot;For Each&quot; XML Flex 3.0 not working
                              knolcollector Level 1
                              Awesome, thanks so much.