13 Replies Latest reply on Nov 2, 2006 8:20 AM by sbryner

    XML id Search

    sbryner Level 1
      Hey delving into XML and have a question. I'd like to create an array with some of the attributes inside my XML elements. I'm probably far from what I'm trying to do and very lost but here is my XML:

      <?xml version="1.0"?>

      <tools>
      <saws id="1">
      <name>Band saw</name>
      <comment>created by craftsman</comment>
      </saws>
      <saws id="2">
      <name>Jig Saw</name>
      <comment>created by Stihl</comment>
      </saws>

      <hammers id="1">
      <name>Big Bertha</name>
      <comment>12 inches long</comment>
      </hammers>

      <hammers id="2">
      <name>Harry Scary Hammer</name>
      <comment>One scary hammer</comment>
      </hammers>
      </tools>



      and here is my try at setting it up so each item is an array element...



        • 1. Re: XML id Search
          sbryner Level 1
          updating my code... now how do I get it to search through each to retrieve only the elements with the id=1 from my XML text?

          how do I remove the properties <name> </name> and just retrieve the info inside....

          <?xml version="1.0"?>

          <tools>
          <saws id="1"> // first I'd like to search id for my variable.... say "1"
          <name>Band saw</name> // or what if I want to retrieve just the text "Band saw"
          <comment>created by craftsman</comment>
          </saws>

          any thoughts?
          • 2. Re: XML id Search
            sbryner Level 1
            ok, updating my file again... along with my question.

            question now:

            1) why when I try to add the XML childNodes from the info I'm searching for (in this case "harry" from code: var toolName = "Harry";)
            into "newTools" array by using the newTools.push(this[e]); it doesn't add anything that I can see when
            tracing (newTools)? Am I not adding in it correctly.

            I'm trying to figure this all out using Kirupa.com's Introduction to XML just need some specific answers.

            thanks for helping,



            • 3. Re: XML id Search
              TimSymons Level 1
              You should use the XPathAPI to help you with the searching.

              mx.xpath.XPathAPI.selectSingleNode(startingNode, "pathToNode");

              So for your example to get the <saws id="1"> node you could use:

              mx.xpath.XPathAPI.selectSingleNode(theTools.firstChild, "/tools/sawTools/saw[@id='1']");

              To get the actual name of the saw at id='1' you could use:

              var tempNode:XMLNode = mx.xpath.XPathAPI.selectSingleNode(theTools.firstChild, "/tools/sawTools/saw[@id='1']");

              trace(mx.xpath.XPathAPI.selectSingleNode(tempNode, "/saws/name").firstChild.nodeValue);

              Tim
              • 4. Re: XML id Search
                sbryner Level 1
                isn't the attached code close to what your saying if I wanted to search for the <name> element of a node?
                I found this with the flash help searching for xpath.

                It doesn't go inside the "for loop" and I don't know why?

                here's the code:

                • 5. Re: XML id Search
                  TimSymons Level 1
                  In the XML example code you posted your toolPath string should be:

                  "/tools/sawTools/saws/name"

                  Also, trace the length of the array to make sure you are getting a number.

                  Tim
                  • 6. Re: XML id Search
                    sbryner Level 1
                    I'm lost. I can't seem to get my search to add to the elements to the length of the (searchResult.length)

                    below is the code I'm working with now. I want to search the "saws" node in the XML file
                    and return all the nodes within the "saws" node. so I used selectNodeList.

                    I used a for loop to loop through and it returns the correct length of the toolArray[/]
                    though when I try to create a new array named
                    searchResult and add the childNodes of
                    all "saws" childNodes it returns only "1".

                    I've increased the XML file to have 4 "saws" elements so the returnSearch.length should = 4

                    also, I can't seem to get it to add nodeValues to the search..
                    How can I get it to look like this?

                    trace(searchResult )

                    returns something like.....

                    resultSearch:Array = {name: "band saw", comment :"The comment here"}];

                    Is this possible?



                    • 7. Re: XML id Search
                      TimSymons Level 1
                      It's close. Here are a couple of ideas.

                      1) It seems that you are trying to re-produce some of the work that the XPathAPI will do for you. Instead of getting just the main node, use the XPathAPI to get all of the nodes you want. For example, if you want all of the <saws> nodes then set your "toolsPath" string to: "/tools/sawTools/saws". The code you used will give you an array of XML nodes. Each array element will be one of the <saws> nodes.

                      2) The result that you posted is an object. The XPathAPI will not return results like that. It will return actually XML nodes. If you want to convert your XML node with attributes into an Object like you showed, then search Adobe's Flash Exchange. It has a couple of code pieces (classes) that will do that once you have the nodes you want.

                      I modified your code to show you how it will return the XML nodes in an array. I also supplied how I thought the XML file looks (let me know if it looks different). The last thing to note is that when you are referencing nodes in your code you are off just a little. I can tell what you are looking for but you are not using the right combinations of firstChild and nodeValue. Use the "trace" function at every step and have it display the results. You may find that the XMLNode properties take a little practice to get them to the path you want.

                      Good luck with your coding.

                      Tim

                      • 8. Re: XML id Search
                        sbryner Level 1
                        thanks, TimSymons.

                        one more question... I found this on the Flash Exchange and it seems to not want to go into the

                        XMLnode.prototype.setVars = function (prevObj) {

                        is there a noticable reason why?
                        • 9. Re: XML id Search
                          TimSymons Level 1
                          Not sure which version of Flash you are using but Flash 7 and higher are case sensitive, so you need to make sure you use:

                          XMLNode

                          and not

                          XMLnode

                          They will mean different things to the compiler.

                          Tim
                          • 10. Re: XML id Search
                            sbryner Level 1
                            Oh, Thanks, I run flash pro 8 and I got this from the exchange and it said it was made in flash 4 I believe.

                            One more question:
                            the code is having an error and maybe this is due to the flash versions but
                            one part of the code in the setVar function seems to be pulling up an error it's
                            the last line in this code:

                            if (this.hasChildNodes() && this.firstChild.nodeType == 1) {
                            if (prevObj == null) {
                            if (eval(this.nodeName) == null) {
                            eval(this.nodeName) = new Array(new Object()); // having problems here!


                            Thanks again, sky


                            Here is the code as a whole:

                            • 11. Re: XML id Search
                              sbryner Level 1
                              Also, I can't seem to get it to trace anything. I don't know why?
                              His example in his Help txt is

                              i.e.

                              <root>
                              <animal>
                              <type>mammal</type>
                              <name>
                              <first>Justin</first>
                              <last>Watkins</last>
                              </name>
                              </animal>
                              </root>

                              would turn into

                              root[0].animal[0].type = mammal;
                              root[0].animal[0].name[0].first = Justin;
                              root[0].animal[0].name[0].last = Watkins;

                              ********************************************************************************

                              If there are several siblings with the same nodeName, the method adds those
                              elements to the end of the array. Since the method uses the for (.. in ..)
                              function all data is stored backwords in the array. The last element is always
                              stored at arrayElement[0] and the first is arrayElement[array.length - 1]

                              I designed this for a very large and complex data object. It is very
                              effecient, and as long as the DTD of the XML is known or you have a basic
                              understanding of the heirarchy of the XML document, the created Flash data
                              object is easy to navigate and find the values.
                              ********************************************************************************

                              Use by creating a new XML object. Load the XML and set the onLoad method to the
                              parseData method.
                              myXML = new XML();
                              myXML.load("version1.xml");
                              myXML.onLoad = myXML.parseData;
                              • 12. Re: XML id Search
                                TimSymons Level 1
                                Sky,

                                I tried to use the code you posted but I am not sure where this information is getting stored. I can trace myXML but is spits out a lot of [object object] items. When I try to access a particular node using the form he listed it did not work, all I got was undefined.

                                I might be able to help more if you could let me know which one you pulled down from the Exchange.

                                Tim
                                • 13. XML id Search
                                  sbryner Level 1
                                  Tim,

                                  Here is the link to what I found it was published by Jwatkins for Flash 5.
                                  I couldn't figure out what the array was called that he created to store the variables.
                                  I removed the (eval) from the line I was having a problem with in the

                                  eval(this.nodeName) = new Array(new Object());
                                  //is now
                                  this.nodeName = new Array(new Object());

                                  I don't get an error when I do this.

                                  Generic XML Parser, V1.0.1 Code:

                                  Hope this helps..

                                  thanks again,
                                  sky