How to check if a node exists in XML file?

New Here ,
Apr 07, 2016 Apr 07, 2016

Copy link to clipboard

Copied

  I have been asked to programme a method for importing data from xml files into our Coldfusion 8 website, I am new to using XML.

My method had been working however I encounted the following error:

Element NETWORKORDER.SECONDARYADDRESS.CONTACTNAME is undefined in MYDOC.

On investigation the node in the XML file was not present.

Is there a method I can use to resolve this error by checking if a node exists in the xml file,  that works with my current code below?

<!--- Read XML file --->
<cffile action="read" file="D:\website\XML\#FileName#" variable="myxml">
<cfset mydoc = XmlParse(myxml)>

<!--- Extract Data from NDS XML File --->
<cfset aPrimaryContactName=(#mydoc.NetworkOrder.PrimaryAddress.ContactName#)>
<cfset aPrimaryContactNumber=(#mydoc.NetworkOrder.PrimaryAddress.ContactNumber#)>
<cfset aPrimaryContactNumber= tostring(#mydoc.NetworkOrder.PrimaryAddress.ContactNumber#)>
<cfset aPrimaryContactAddressLine1=(#mydoc.NetworkOrder.PrimaryAddress.line1#)>
<cfset aPrimaryContactAddressLine2=(#mydoc.NetworkOrder.PrimaryAddress.line2#)>
<cfset aPrimaryContactAddressLine3=(#mydoc.NetworkOrder.PrimaryAddress.line3#)>
<cfset aPrimaryContactAddressLine4=(#mydoc.NetworkOrder.PrimaryAddress.line4#)>
<cfset aPrimaryContactAddressLine5=(#mydoc.NetworkOrder.PrimaryAddress.line5#)>
<cfset aPrimaryContactCity=(#mydoc.NetworkOrder.PrimaryAddress.City#)>
<cfset aPrimaryContactPostcode=(#mydoc.NetworkOrder.PrimaryAddress.PostalCode#)>
<cfset aPrimaryContactCounty=(#mydoc.NetworkOrder.PrimaryAddress.CountrySubdivision#)>
<cfset aPrimaryContactCountryCode=(#mydoc.NetworkOrder.PrimaryAddress.CountryCode#)>
<cfset aPrimaryContactCountry=(#mydoc.NetworkOrder.SecondaryAddress.Country#)>
<cfset aSecondaryContactName=(#mydoc.NetworkOrder.SecondaryAddress.ContactName#)>
<cfset aSecondaryAttentionOfName=(#mydoc.NetworkOrder.SecondaryAddress.AttentionOfName#)>
<cfset aSecondaryContactNumber=(#mydoc.NetworkOrder.SecondaryAddress.ContactNumber#)>
<cfset aSecondaryContactAddressLine1=(#mydoc.NetworkOrder.SecondaryAddress.line1#)>
<cfset aSecondaryContactAddressLine2=(#mydoc.NetworkOrder.SecondaryAddress.line2#)>
<cfset aSecondaryContactAddressLine3=(#mydoc.NetworkOrder.SecondaryAddress.line3#)>
<cfset aSecondaryContactAddressLine4=(#mydoc.NetworkOrder.SecondaryAddress.line4#)>
<cfset aSecondaryContactAddressLine5=(#mydoc.NetworkOrder.SecondaryAddress.line5#)>
<cfset aSecondaryContactCity=(#mydoc.NetworkOrder.SecondaryAddress.City#)>
<cfset aSecondaryContactPostcode=(#mydoc.NetworkOrder.SecondaryAddress.PostalCode#)>
<cfset aSecondaryContactCounty=(#mydoc.NetworkOrder.SecondaryAddress.CountrySubdivision#)>
<cfset aSecondaryContactCountryCode=(#mydoc.NetworkOrder.SecondaryAddress.CountryCode#)>
<cfset aSecondaryContactCountry=(#mydoc.NetworkOrder.SecondaryAddress.Country#)>

Views

655

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Advocate , Apr 07, 2016 Apr 07, 2016
Since you already have the xml parsed into a document, the easiest is to use xmlSearch(). Take a look at the doc, it's pretty easy to use. Something as simple as:    <cfset a = xmlSearch(mydoc,"//NetworkOrder/PrimaryAddress/ContactName" ) />To more complex like:    <cfset a = xmlSearch(mydoc,"//*[local-name()='ContactName']" ) />And in either case:    <cfif arrayLen(a)> <!--- something found ---> </cfif>Read the doc and google is your friend "coldfusion xmlsearch"

Likes

Translate

Translate
Advocate ,
Apr 07, 2016 Apr 07, 2016

Copy link to clipboard

Copied

Since you already have the xml parsed into a document, the easiest is to use xmlSearch(). Take a look at the doc, it's pretty easy to use. Something as simple as:

    <cfset a = xmlSearch(mydoc,"//NetworkOrder/PrimaryAddress/ContactName" ) />

To more complex like:

    <cfset a = xmlSearch(mydoc,"//*[local-name()='ContactName']" ) />

And in either case:

    <cfif arrayLen(a)> <!--- something found ---> </cfif>

Read the doc and google is your friend "coldfusion xmlsearch"

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 08, 2016 Apr 08, 2016

Copy link to clipboard

Copied

LATEST

Thank you Steve for pointing me in the right direction. Somtimes when you google for information you can't see the forest for the trees.

Your simple method works perfectly.

<cfset a = xmlSearch(mydoc,"//NetworkOrder/PrimaryAddress/ContactName" ) />

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines