Solved

xmlSearch and xPath

Posted on 2011-09-09
7
406 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

13 Experts available now in Live!

Get 1:1 Help Now