Solved

Error Testing if XML Element Exists (with isXmlElem)

Posted on 2009-05-08
3
355 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
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

Suggested Solutions

Title # Comments Views Activity
XML response optional elements 12 56
PowerShell script to remove string in xml file 3 40
PHP delete contents of file- before writing to it 6 49
parsing xml using powershell 6 34
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…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

735 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