Solved

using coldfusion to access xml namespaced elements

Posted on 2011-09-14
10
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

734 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