4 Replies Latest reply on Mar 16, 2007 7:30 AM by insuractive

    XML Node Extraction

    RobCepek
      Okay

      I have what I think is a simple question.

      If i have a XML file that looks like the following

      How would I extract the
      <RecordSet Count="177" TotalCount="177">
      count value and totalcount value from the XML file to store as a variable for later use?

      I was trying to do the following but could not figure out the correct syntax

      <cfset XMLSessionID = xmlparse(cfhttp.FileContent)>
      <cfset FileName = #XMLSessionID.UpShotResponse.Select.xmltext#> (it's incomplete i know)

      Just looking for a way to grab that attribute out of the xml value?

      Thanks in advance

      Rob
        • 1. Re: XML Node Extraction
          Level 7
          you would use the xmlAttributes structure on the node you want the
          information from.

          I find <cfdump var="#XMLSessionID#"> to be most usefully when traversing
          XML documents.
          • 2. Re: XML Node Extraction
            insuractive Level 3
            I second Ian's suggestion. <cfdump>-ing XML docs has saved me more than once from pulling my hair out when dealing with XML in CF.
            • 3. Re: XML Node Extraction
              BKBK Adobe Community Professional & MVP
              The are no end tags, so I would assume element RecordSet is within element Select and Select is within UpShotResponse. Then some possibilities are

              XMLSessionID.XmlRoot["Select"]["RecordSet"].XmlAttributes["Count"]
              XMLSessionID["UpShotResponse"]["Select"]["RecordSet"].XmlAttributes["Count"]


              • 4. Re: XML Node Extraction
                insuractive Level 3
                I should probably also mention, if you are worried about the format of your XML file changing, or will be dealing with repeating elements, you may also want to take a look at the CF XMLSearch() function. It allows you to use XPath to specify what XML elements you are looking for and typically returns the data as an array. The syntax is something like this:

                XMLSearch(XMLSessionID, "//RecordSet")

                XPath is a pretty powerful way to retreive data from XML, though if you are just looking for 1 element in an unchanging XML document, it might not be the most efficient thing to use.