Solved

using coldfusion to access xml namespaced elements

Posted on 2011-09-14
10
425 Views
Last Modified: 2012-05-12
Hi,

Is it possible to return a structure of all data returned in any namespaced element /node. Please see attached code.

I am new to xml so please forgive my mixup in terminology

Regards
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
Comment
Question by:Lmillard
  • 4
  • 3
  • 3
10 Comments
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 36535313
Just use xmlParse

<cfsavecontent variable="returnedVar">
<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>
</cfsavecontent>



<cfdump var="#xmlParse(returnedVar)#">

Open in new window

0
 

Author Comment

by:Lmillard
ID: 36535451
Sorry, I wasn't very clear, I just want to grab anything within a particular namespace, in this case ns3 (is that even a namespace??)
0
 
LVL 11

Accepted Solution

by:
Brijesh Chauhan earned 250 total points
ID: 36535941
You can use XML search to find the element, I am for some reason not able to find ns3: record if I try to search, probably the XPATH expression needs to be corrected.

You can also loop to get the element values that you are looking for...

<cfset arrayResult = xmlSearch(returnedVar,'/env:Envelope/env:Body/*') />

<cfloop from="1" to="#arraylen(arrayResult[1].XmlChildren[1].XmlChildren)#" index="i">
	<cfset ns3_ResourceRecord = arrayResult[1].XmlChildren[1].XmlChildren[i].XmlAttributes />
    <cfdump var="#ns3_ResourceRecord#">
</cfloop>

Open in new window

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 36535943
<cfset arrayResult = xmlSearch(returnedVar,'/env:Envelope/env:Body/*') />

<cfloop from="1" to="#arraylen(arrayResult[1].XmlChildren[1].XmlChildren)#" index="i">
	<cfset ns3_ResourceRecord = arrayResult[1].XmlChildren[1].XmlChildren[i].XmlAttributes />
    <cfdump var="#ns3_ResourceRecord#">
</cfloop>

Open in new window

0
 

Author Comment

by:Lmillard
ID: 36536303
This does bring back the resourceRecord elements but not the infoValues. Pretty sure the code could be amended to do this but I need to search on namespace (ns3) as there is a variation of layouts coming back to me
0
DevOps Toolchain Recommendations

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

 
LVL 52

Expert Comment

by:_agx_
ID: 36536542
<cfset result = xmlSearch(xmlDoc, "//*[namespace-uri()='http://schema.xxx.com/v01/']")>
0
 

Author Comment

by:Lmillard
ID: 36537961
I have added the code above but am still getting some odd results, the arrayLen is 4 now
<cfset xmlDoc = '<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>'>

<!--- switch between arrayResult retrieval methods --->
<cfset arrayResult = xmlSearch(xmlDoc,'/env:Envelope/env:Body/*') />
<cfset arrayResult = xmlSearch(xmlDoc, "//*[namespace-uri()='http://schema.xxx.com/v01/']")>

<cfloop from="1" to="#arraylen(arrayResult[1].XmlChildren[1].XmlChildren)#" index="i">
	<cfset ns3_ResourceRecord = arrayResult[1].XmlChildren[1].XmlChildren[i].XmlAttributes />
    <cfdump var="#ns3_ResourceRecord#">
</cfloop>


<cfdump var="#arrayResult#">

Open in new window

0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 250 total points
ID: 36538102
>> [namespace-uri()='http://schema.xxx.com/v01/']"

That will extract all elements with the "ns3" prefix - including the child InfoValues  elements.  If you only want to retrieve the parent element  ie "ResourceRecord", add a filter on local-name:

<cfset arrayResult = xmlSearch(xmlDoc, "//*[local-name() = 'ResourceRecord' and namespace-uri()='http://schema.xxx.com/v01/']")>

Open in new window


Then you can extract the attributes just like you were doing before:

<cfset results = []>
<cfloop array="#nodeArray#" 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: 36538155
Oops.. It would be nice if the array name matched too ;-)

<cfset nodeArray = xmlSearch(xmlDoc, "//*[local-name() = 'ResourceRecord' and namespace-uri()='http://schema.xxx.com/v01/']")>
....
<cfloop array="#nodeArray#" index="node">
...
0
 

Author Closing Comment

by:Lmillard
ID: 36538215
Thank you very much to both of you, an elegant solution which works over the scenarios I have come across to date. much appreciated.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

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

23 Experts available now in Live!

Get 1:1 Help Now