Solved

Error Testing if XML Element Exists (with isXmlElem)

Posted on 2009-05-08
3
357 Views
Last Modified: 2013-11-18

I'm parsing an XML API ... and all works great ... except on the rare occasion a specific 'branch' (for lack of correct XML terminology) fails to exist ... in which case I get this error:

Element NEWSNEWSCOLLECTIONS.NEWSNEWSCOLLECTION.NEWSNEWSARTICLES.NEWSNEWSARTICLE is undefined in a Java object of type class coldfusion.xml.XmlNodeMap referenced as ''

I'd like to TEST if element this element exists:
<cfset collections=IsXmlElem(live_doc.SearchResponse.newsNews.newsResults.newsNewsResult[i].newsNewsCollections.newsNewsCollection.newsNewsArticles.newsNewsArticle)>

AND if 'collections' is TRUE ... then I can get its arraylen and do fun stuff ... else move on.

arraylen(live_doc.SearchResponse.newsNews.newsResults.newsNewsResult[i].newsNewsCollections.newsNewsCollection.newsNewsArticles.newsNewsArticle)


ALAS ... the test - isXmlElem reports TRUE ... when TRUE - which is great!~ but I get an ERROR when clearly it is false - instead of NO error an 'just' telling var collections = FALSE.

Does this make sense and can you help?

Richard
0
Comment
Question by:rcbuchanan
[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
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 24338227
Try using structKeyExists first, to verify the key exists.  If it does, then do the rest of your logic.
<cfif structKeyExists( pathNotIncludingTheyKeyYouAreTesting,  "KeyName")>
    ... do something ...
</cfif>

ie Something like:

<cfif structKeyExists(ive_doc.SearchResponse.newsNews.newsResults.newsNewsResult[i].newsNewsCollections.newsNewsCollection.newsNewsArticles, "NEWSNEWSARTICLE")>
    ... do something ...
</cfif>

0
 

Author Comment

by:rcbuchanan
ID: 24338765
Worked perfectly. Thank you (again).
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24338929
You're welcome :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

617 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