Solved

xmlSearch and xPath

Posted on 2011-09-09
7
409 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Lmillard
  • 4
  • 3
7 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 36511328
If you also want all of the attributes:

<!--- get "result" node --->
<cfset resultArray = xmlSearch(yourXMLString, "//result")>

<!--- create structure for storing results --->
<cfset data = structNew()>
<!--- assuming there's only 1 result node. --->
<cfloop array="#resultArray[1].XMLChildren#" index="node">
        <!--- extract all child fields and their attributes --->
      <cfset elemName = listLast(node.xmlName, ":")>
      <cfset data[elemName] = duplicate(node.xmlAttributes)>
</cfloop>

<!--- debugging display whatever values you want .... --->
<cfoutput>
#data.DefaultAccountPreference.defaultAccount#<br>
#data.DefaultDateAndTimePreference.DateFormat#<br>
#data.DefaultDateAndTimePreference.TimeFormat#<br>
... etc...
</cfoutput>

<!--- debugging, show full results --->
<cfdump var="#data#">
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36511653
Add validation as needed.  If the fields/attributes can vary use structKeyExists() before accessing the structure elements.
0
 

Author Closing Comment

by:Lmillard
ID: 36511709
Awesome solution thanks! Works perfectly.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Lmillard
ID: 36516124
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

0
 
LVL 52

Expert Comment

by:_agx_
ID: 36517806
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

0
 
LVL 52

Expert Comment

by:_agx_
ID: 36520380
>> 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.
0
 

Author Comment

by:Lmillard
ID: 36528523
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now