Solved

Error Testing if XML Element Exists (with isXmlElem)

Posted on 2009-05-08
3
345 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
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
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
Comment Utility
Worked perfectly. Thank you (again).
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
You're welcome :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

771 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

14 Experts available now in Live!

Get 1:1 Help Now