Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error Testing if XML Element Exists (with isXmlElem)

Posted on 2009-05-08
3
Medium Priority
?
358 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 2000 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

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

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…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This video teaches viewers about errors in exception handling.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

730 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