3 Replies Latest reply on Jun 19, 2007 8:41 AM by spotsstang

    Help with xmlsearch

      I have been trying to get this final step completed for my application and just cannot seem to get a answer. I need to retriev a value from a xml post.

      I am using xmlsearch to find the result:

      <cfset customer.firstname = xmlsearch(xmlcustomer, "name[@part='first']")>
      <cfset customer.lastname = xmlsearch(xmlcustomer, "name[@part='last']")>

      I am getting returned:
      <?xml version="1.0" encoding="UTF-8"?> <name part="first">MyFirstName</name>
      <?xml version="1.0" encoding="UTF-8"?> <name part="last">MyLastName</name>

      I obviously want just the first and last names but cannot seem to figure out how to get them out. Any help would be greatly appreciated.
        • 1. Re: Help with xmlsearch
          insuractive Level 3
          xmlSearch() returns an XML node, which is a complex data type (think structure). Once you retrieve your XML node, you then have to parse out the data you want (do a <cfdump> on your xmlsearch() results to see what I mean).

          The solution is pretty simple:
          <cfset xmlNode= xmlsearch(xmlcustomer, "name[@part='first']")>
          <cfset customer.firstname = xmlNode.xmlText>
          • 2. Re: Help with xmlsearch
            Hi spotsstang,

            Here is a simple example, I want to search a employee with id of 23

            <employee id="23">

            <cfset mydump = xmlSearch(myXml,'/employee [@id='23']/*') >
            <cfdump var="#mydump #">

            see the dump value, it will show you the returned structure.

            hopefully this will help you in understanding of xmlsearch.
            • 3. Re: Help with xmlsearch
              spotsstang Level 1
              Thanks for the replies. I was able to work through it in the mean time and here is the way that I resolved it:

              <cfset fName = xmlsearch(xmlcustomer, "name[@part='first']")>
              <cfset xmlFName = xmlparse(fName[1][1], "yes")>
              <cfset customer.firstname = xmlFName["name"]["XmlText"]>

              <cfset lName = xmlsearch(xmlcustomer, "name[@part='last']")>
              <cfset xmlLName = xmlparse(lName[1][1], "yes")>
              <cfset customer.lastname = xmlLName["name"]["XmlText"]>