?
Solved

using coldfusion to access xml namespaced elements

Posted on 2011-09-14
10
Medium Priority
?
437 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 1000 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
Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

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

Technology Partners: 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!

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…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

777 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