Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error Testing if XML Element Exists (with isXmlElem)

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

876 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