Looping Over XML Files Issue

Trying to read in a number of XML files loop over them and display them in a  table format, each file relates to one record

The Code So Far which doesnt work very well (Crashes out when one xml file doesnt have any data for a particulur XML field node !?)

<cfoutput query="Get_Shortlist_Candidates" >
               
   <cfset myFile= getFileFromPath("#Get_Shortlist_Candidates.CP_PROFILE_FN#")>

   <cffile action="read" file="\\192.168.0.120\#myfile#" variable="xml">

   <cfset xmldoc = XmlParse(xml)>
               
   <tr>  
     <td>
          <cfif structKeyexists(xmlDoc.Resume.StructuredXMLResume.ContactInfo.PersonName, "FormattedName" ) >
                 #xmlDoc.Resume.StructuredXMLResume.ContactInfo.PersonName.FormattedName.XmlText#
          </cfif>
     </td>
     <td>
          <cfif structKeyexists(xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile, "FormattedNumber" ) >
                 #xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile.FormattedNumber.XmlText#
          </cfif>
     </td>
                   
   </tr>
</cfoutput>

Help !

JT
jturkingtonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CFDevHeadCommented:
What is the error you are getting? And can you post the xml file?
trailblazzyr55Commented:
<cfoutput query="Get_Shortlist_Candidates" >       
   <cfset myFile= #getFileFromPath(CP_PROFILE_FN[currentrow])#>
   <cffile action="read" file="\\192.168.0.120\#myfile#" variable="xml">
   <cfset xmldoc = XmlParse(xml)>
               
   <tr>  
     <td>
          <cfif structKeyexists(xmlDoc.Resume.StructuredXMLResume.ContactInfo.PersonName, "FormattedName" ) >
                 #xmlDoc.Resume.StructuredXMLResume.ContactInfo.PersonName.FormattedName.XmlText#
                         <cfelse>
                         No Records
          </cfif>
     </td>
     <td>
          <cfif structKeyexists(xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile, "FormattedNumber" ) >
                 #xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile.FormattedNumber.XmlText#
                         <cfelse>
                         No Records
          </cfif>
     </td>
                   
   </tr>
</cfoutput>
jturkingtonAuthor Commented:
Nope still getting the error on the second row at mobile:  -

Checked the second row XML file true enough it doesnt have a mobile xml field ?!?


Element RESUME.STRUCTUREDXMLRESUME.CONTACTINFO.CONTACTMETHOD.MOBILE is undefined in XMLDOC.

Any other ideas ?

is it still trying to read the first row XMLDOC variable maybe i need to set that up in some type of array xmldoc[i] ??

Cheers

JT
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

trailblazzyr55Commented:
actually you don't need #'s here either...

<cfset myFile= #getFileFromPath(CP_PROFILE_FN[currentrow])#>

change to...

<cfset myFile= getFileFromPath(CP_PROFILE_FN[currentrow])>
trailblazzyr55Commented:
also try a little debug so you can see what's going on...

<cfdump var="#xmldoc#">

see if that helps spot your problem
trailblazzyr55Commented:
<cfoutput query="Get_Shortlist_Candidates" >       
   <cfset myFile= #getFileFromPath(CP_PROFILE_FN[currentrow])#>
   <cffile action="read" file="\\192.168.0.120\#myfile#" variable="xml">
   <cfset xmldoc = XmlParse(xml)>
               
   <tr>  
     <td>
               <cfif isdefined(xmlDoc.Resume.StructuredXMLResume.ContactInfo.PersonName)>
                  <cfif structKeyexists(xmlDoc.Resume.StructuredXMLResume.ContactInfo.PersonName, "FormattedName" ) >
                 #xmlDoc.Resume.StructuredXMLResume.ContactInfo.PersonName.FormattedName.XmlText#
                         <cfelse>
                         No Records
                </cfif>
                  <cfelse>              
              </cfif>
         
     </td>
     <td>
               <cfif isdefined(xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile)>
            <cfif structKeyexists(xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile, "FormattedNumber" ) >
                 #xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile.FormattedNumber.XmlText#
                         <cfelse>
                         No Records
            </cfif>
                  <cfelse>
              </cfif>
     </td>
                   
   </tr>
</cfoutput>
jturkingtonAuthor Commented:
Nope error:  -

Parameter 1 of function IsDefined, which is now &quot;coldfusion.xml.XmlNodeList@19bcae1&quot;, must be a syntactically valid variable name.
jturkingtonAuthor Commented:
The <cfif structKeyexists...........

works fine if i loop through fields in the one XML file, why doesnt it work for multiple XML files ??

JT
jturkingtonAuthor Commented:
It looks like the original code works it displays two rows:  -


Candidate                            Mobile
Albert Dickinson                   +44 484839230

Mr Jonathan Turkington

Element RESUME.STRUCTUREDXMLRESUME.CONTACTINFO.CONTACTMETHOD.MOBILE is undefined in XMLDOC.


It looks like its passing true for the second looped : -

<cfif structKeyexists(xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile, "FormattedNumber" ) >

This would indicate its still reading the first rows <cfset xmldoc = XmlParse(xml)>   right ????


Any Ideas ?

JT
jturkingtonAuthor Commented:
Sorry forgot to mention that Mr Jonathan Turkington doesnt have a mobile xmlfield in his XML file.....
rob_lorentzCommented:

isDefined takes a sting....

<cfif isdefined("xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile")>
jturkingtonAuthor Commented:
Thanks rob_lorentz is seem to work now

I take it i should be only using the <cfif isdefined> and remove the <cfif structKeyexists> as both do the same thing ??

Cheers

JT
trailblazzyr55Commented:
<td>
       <cfif isdefined(xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile.FormattedNumber)>
                   #xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile.FormattedNumber.XmlText#
          <cfelse>
                    No Records
      </cfif>
          <cfelse>
      </cfif>
</td>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
umbraeCommented:

<cfoutput query="Get_Shortlist_Candidates" >
               
   <cfset myFile= getFileFromPath("#Get_Shortlist_Candidates.CP_PROFILE_FN#")>

   <cffile action="read" file="\\192.168.0.120\#myfile#" variable="xml">

   <cfset xmldoc = XmlParse(xml)>
               
   <tr>  
     <td>
          <cfif isDefined('xmlDoc.Resume.StructuredXMLResume.ContactInfo.PersonName.FormattedName') >
                 #xmlDoc.Resume.StructuredXMLResume.ContactInfo.PersonName.FormattedName.XmlText#
          </cfif>
     </td>
     <td>
          <cfif isDefined('xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile.FormattedNumber') >
                 #xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile.FormattedNumber.XmlText#
          </cfif>
     </td>
                   
   </tr>


Runs a little slower (isDefined is kinda beefy) but it won't crash out when it can't find a parent scope.
trailblazzyr55Commented:
How does that differ from what's been posted?
umbraeCommented:
Heres a hint: it works. :P

Your last comment:
isdefined(xmlDoc.Resume.StructuredXMLResume.ContactInfo.ContactMethod.Mobile.FormattedNumber)

causes an error, because it doesn't have the varname quoted.

-Umbrae
trailblazzyr55Commented:
LOL =) good point! completely missed the quotes... duhhh!
jturkingtonAuthor Commented:
Thanks umbrae

Can you input anyuthing into the following question basically i am trying to output Telephone numbers with an attribute type "Home" and "Work" but it aint having it !

http://www.experts-exchange.com/Web/WebDevSoftware/ColdFusion/Q_21486950.html#14421997

Cheers

JT
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.