• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1043
  • Last Modified:

Check if XML Node Exists

In my previous question:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Cold_Fusion_Markup_Language/Q_24131047.html?cid=239#a23605044

I had a XML file that I loop through and it winds up changing towards the end.  I want to know if there is a way to check if a node exists, if it does, continue the loop, if not, change to the next loop.  

Please view XML file in previous question.
<CFHTTP url="URL HERE" method="get">
<cfset xmlDoc = XMLParse(cfhttp.filecontent)>
<cfoutput>
  <cfloop from="1" to="#ArrayLen(xmlDoc.Demographics.response.pages.page)#" index="i">
    <strong>#xmlDoc.Demographics.response.pages.page[i].name.xmlText#</strong><br>
    <cfloop from="1" to="#ArrayLen(xmlDoc.Demographics.response.pages.page[i].tables.table)#" index="x">
      #xmlDoc.Demographics.response.pages.page[i].tables.table[x].name.xmlText#<br>
      <cfloop from="1" to="#ArrayLen(xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute)#" index="y">
        #xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].name.xmlText#<br>
        <cfloop from="1" to="#ArrayLen(xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].values)#" index="z">
         Neighborhood: #xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].values[z].neighborhood.value.xmlText#<br>
         City: #xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].values[z].city.value.xmlText#<br>
         Nation: #xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].values[z].nation.value.xmlText#<br><br>
        </cfloop>
      </cfloop>
    </cfloop>
    <br>
    <br>
  </cfloop>
</cfoutput>

Open in new window

0
jeffmace
Asked:
jeffmace
  • 2
1 Solution
 
Andrew MaurerCommented:
Can you add the url to this so I can test it on my local before i submit an answer?
0
 
Andrew MaurerCommented:
there is attribute[y].values and attribute[y].value

that may need further testing that is returning everything you'd expect... do a dump of the xml doc at the end to verify it returns all you need
<cfoutput>
  <cfloop from="1" to="#ArrayLen(xmlDoc.Demographics.response.pages.page)#" index="i">
    <strong>#xmlDoc.Demographics.response.pages.page[i].name.xmlText#</strong><br>
    <cfloop from="1" to="#ArrayLen(xmlDoc.Demographics.response.pages.page[i].tables.table)#" index="x">
      #xmlDoc.Demographics.response.pages.page[i].tables.table[x].name.xmlText#<br>
      <cfloop from="1" to="#ArrayLen(xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute)#" index="y">
        #xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].name.xmlText#<br>
		<cfif structkeyexists(xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y],"values")>
			<cfloop from="1" to="#ArrayLen(xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].values)#" index="z">
				Neighborhood: #xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].values[z].neighborhood.value.xmlText#<br>
				City: #xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].values[z].city.value.xmlText#<br>
				Nation: #xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].values[z].nation.value.xmlText#<br><br>
			</cfloop>
		<cfelseif structkeyexists(xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y],"value")>
			<cfloop from="1" to="#ArrayLen(xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].value)#" index="z">
				#xmlDoc.Demographics.response.pages.page[i].tables.table[x].data.attribute[y].value.xmltext#<br>
			</cfloop>
		</cfif>
      </cfloop>
    </cfloop>
    <br>
    <br>
  </cfloop>
</cfoutput>

Open in new window

0
 
jeffmaceAuthor Commented:
Absolutely SUPERB!!!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now