Link to home
Start Free TrialLog in
Avatar of Lmillard
Lmillard

asked on

xmlSearch and xPath

Hi,

I have a variery of xml structures being returned from SOAP requests so am using xmlSearch() and xPath to extract structures of data however the attached structure is causing problems as I wish to retrieve all fields within the <result> part but am not sure how.

Any help much appreciated

cheers

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header/>
   <env:Body>
      <v01:getUserDefaultPreferencesResponse xmlns:v01="http://webservice.api.xxx.com/v01/">
         <result xmlns:ns2="http://webservice.api.xxx.com/v01/" xmlns:ns3="http://schema.xxx.com/v01/">
            <ns3:DefaultAccountPreference defaultAccount="01023A6D084"/>
            <ns3:DefaultDateAndTimePreference TimeZone="GMT" DateFormat="MM/DD/YYYY" TimeFormat="12 hour" TTLFormat="Standard"/>
            <ns3:DefaultNumberOfrecordsPreference LargeTable="50" SmallTable="50"/>
            <ns3:DeleteConfirmPreference DeleteConfirmation="Confirm"/>
            <ns3:AutomaticPointerPreference ptrStatus="Disabled"/>
            <ns3:DefaultReportPreference ReportType="Overall Activity - Domains" GraphType="Bar"/>
         </result>
      </v01:getUserDefaultPreferencesResponse>
   </env:Body>
</env:Envelope>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Add validation as needed.  If the fields/attributes can vary use structKeyExists() before accessing the structure elements.
Avatar of Lmillard
Lmillard

ASKER

Awesome solution thanks! Works perfectly.
Hi agx, not sure if you will see this comment, if not I will repost on Monday but in the mean time.

I have a new twist on the xml structure where the depth is mroe than 1 level of results. now includes xmlChildren. is it possible to amend your example to handle the attached code?

Cheers
Leigh
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header/>
   <env:Body>
      <v01:getResourceRecordsOfZoneResponse xmlns:v01="http://webservice.api.xxx.com/v01/">
         <ResourceRecordList xmlns:ns2="http://webservice.api.xxx.com/v01/" xmlns:ns3="http://schema.xxx.com/v01/">
            <ns3:ResourceRecord ZoneName="yyy.co.uk." Type="1" DName="www.yyy.co.uk." TTL="14400" Guid="04023A7008CE3817" ZoneId="03023A7008CE36C9" LName="www.yyy.co.uk." Created="2010-12-16T15:39:32.000Z" Modified="2010-12-16T15:39:32.000Z">
               <ns3:InfoValues Info1Value="82.153.226.171"/>
            </ns3:ResourceRecord>
            <ns3:ResourceRecord ZoneName="yyy.co.uk." Type="1" DName="mx.yyy.co.uk." TTL="14400" Guid="04023A7008CE3991" ZoneId="03023A7008CE36C9" LName="mx.yyy.co.uk." Created="2010-12-16T15:44:18.000Z" Modified="2010-12-16T15:44:18.000Z">
               <ns3:InfoValues Info1Value="82.153.226.171"/>
            </ns3:ResourceRecord>
         </ResourceRecordList>
      </v01:getResourceRecordsOfZoneResponse>
   </env:Body>
</env:Envelope>

Open in new window

There's actually another twist.  Since there's multiple "ResourceRecord" elements you can't use a structure. You'd need to use an array instead.

Keep in mind it only handles 2 levels. If you expect to go beyond that, you might be better off w/a recursive function.

<cfset resultArray = xmlSearch(xmlDoc, "//ResourceRecordList")>
<cfset results = []>
<cfloop array="#resultArray[1].XMLChildren#" index="node">
	  <cfset nodeData = duplicate(node.xmlAttributes)>
	  <cfloop array="#node.XMLChildren#" index="childNode">
          <cfset elemName = listLast(childNode.xmlName, ":")>
          <cfset nodeData[elemName] = duplicate(childNode.xmlAttributes)>
      </cfloop>
	  <cfset arrayAppend(results, nodeData)>
</cfloop>

<cfdump var="#results#">

Open in new window

>> Since there's multiple "ResourceRecord" elements you can't use a structure

Well .. you could. You just can't use the node name ie "ResourceRecord" as the structure key.  Structure key names must be unique. Otherwise, you'll overwrite the information and end up with only 1 ResourceRecord.
Hi,
Sorry for delay in response, I was away for a day or two.

This worked really well for me and returned everything perfectly. I am hoping that it only goes two levels deep but I keep getting surprises as I move through more SOAP requests on this project.

Thanks for your time on this, much appreciated. I think I will be doing some reading up on xml from here on.