• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to check whether Nodes exist in the XML?

Guest
Aug 17, 2009 Aug 17, 2009

Copy link to clipboard

Copied

Hi,

Is there a way to check if Nodes exist in XML document, I would like to do as following:

If nodes exist, do something else do something else.

So far, I got error when I try to read the missing nodes.

Thanks in advance and I am still on old Coldfusion MX 6.1 version.

Best regards,

Ericfound

TOPICS
Advanced techniques

Views

1.7K

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
Engaged ,
Aug 17, 2009 Aug 17, 2009

Copy link to clipboard

Copied

You want to use the XmlSearch() function to perform an "XPath query" of the XML object tree.  (This happens to be a function that has changed considerably in recent versions of CF, so it would seem from the published documentation.)

"XPath" is basically a search-protocol that returns a list of matching nodes, sparing you the sometimes-VERY-dirty work of trying to navigate the tree yourself with fixed logic.  You will still probably have to iterate through the list to verify that the matching nodes really are "the 'droids you're looking for."

Votes

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
Guest
Aug 17, 2009 Aug 17, 2009

Copy link to clipboard

Copied

Thanks TLCth, I did tried on XMLSearch but it return as error too due to that nodes is dynamically. It will appear the nodes if there is a value else the nodes is not populate at last.

My XML look something like this:

<myxml>

     <cat_a>

          <sub_a1></sub_a1>

          <sub_a2></sub_a2>

          <sub_a3></sub_a3>

          <sub_a4>

               <subgrp_a4a></subgrp_a4a>

                <subgrp_a4b></subgrp_a4b>

          </sub_a4>

     </cat_a>

</myxml>

On subgrp_a4b is dymanically depending on another system, if there is a value it would populate this nodes else where is not subgrp_a4b nodes.

This is my major issue.

You are saying XPathth query, could u give me some example coding?

Votes

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
Engaged ,
Aug 17, 2009 Aug 17, 2009

Copy link to clipboard

Copied

I don't have examples that would be apropos to your situation, but I'm sure that others in these forums will be in a position to provide them.

Most of the time, nodes are considered to be optional:  "if there is no meaningful data to put 'there,' then there will be nothing 'there.' "  So, in reference to your example, if there is no data to be put into subgrp_a4b, that entire node might not exist ... and if there's no data for any part of sub_a4, then it, too, might not exist, and so-on.

The general approach that you should take might be to query for "/myxml/cat_a/sub_a4/subgrp_a4a/" and ... "hey, it might produce a non-empty list or it might not."  Either way, if there is something there, you've got a list of what's there, and if there isn't, you've got an empty list.

On the other hand, you might take the approach of "show me the money!"    By that I mean, search for the target node that you're looking for, anywhere in (say...) "/myxml."   This will do all the searching for you:  it's up to the XPath engine to search the entire subtree and to locate the information that you want to find, no matter where it is.  Once you have the result-list, you can use XPath once again (separately...) to "find out where it was."  That is, to obtain the path from the root of the tree to each node that XPath found for you.

With that approach, it wouldn't matter how many "cats" or "subs" or "subgrps" might be present in the XML stream:  the XPath engine will do all of the tree-traversal for you, to produce a complete list of all of the nodes that match what you're looking for.  Each element in the list is a reference to the actual corresponding object in the XML tree, and therefore can be used to ascertain its location in the tree.


"XPath" stands right there alongside "regular expressions" as being one of those "hugely time-saving tools" that you can easily overlook but that you simply must "get to know!"  (The hair-follicles that you save will be your own.)

Votes

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
Community Expert ,
Aug 22, 2009 Aug 22, 2009

Copy link to clipboard

Copied

On subgrp_a4b is dymanically depending on another system, if there is a value it would populate this nodes else where is not subgrp_a4b nodes.

That's somewhat ambiguous. Do you mean that, sometimes the XML document contains the element subgrp_a4b and sometimes not? Or, do you mean that it sometimes contains XmlText, like the xxx in   <subgrp_a4b>xxx</subgrp_a4b>, and sometimes it doesn't?

Votes

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
Community Expert ,
Aug 22, 2009 Aug 22, 2009

Copy link to clipboard

Copied

An example:

<cfsavecontent variable="myxmldoc"><myxml>
         <cat_a>
              <sub_a1></sub_a1>
              <sub_a2></sub_a2>
              <sub_a3></sub_a3>
              <sub_a4>
                   <subgrp_a4a>blah blah blah</subgrp_a4a>
                   <subgrp_a4b></subgrp_a4b>
              </sub_a4>
         </cat_a>
    </myxml>
</cfsavecontent>
 
<cfset myxmldoc = XmlParse(myxmldoc)>

<cfset subgrp_a4a_search_array = XmlSearch(myxmldoc, "//*[local-name()='subgrp_a4a']")>
<cfset subgrp_a4b_search_array = XmlSearch(myxmldoc, "//*[local-name()='subgrp_a4b']")>
<cfset subgrp_a4c_search_array = XmlSearch(myxmldoc, "//*[local-name()='subgrp_a4c']")>

<cfif arrayLen(subgrp_a4a_search_array) GT 0>
    <div>Element subgrp_a4a exists.</div>
    <cfset subgrp_a4a_content = subgrp_a4a_search_array[1].XMLText>
    <div>subgrp_a4a_content: <cfoutput>#subgrp_a4a_content#</cfoutput></div>
<cfelse>
    <div>Element subgrp_a4a doesn't exist.</div>
</cfif>
<br>
<cfif arrayLen(subgrp_a4b_search_array) GT 0>
    <div>Element subgrp_a4b exists.</div>
    <cfset subgrp_a4b_content = subgrp_a4b_search_array[1].XMLText>
    <div>subgrp_a4b_content: <cfoutput>#subgrp_a4b_content#</cfoutput></div>
<cfelse>
    <div>Element subgrp_a4b doesn't exist.</div>
</cfif>
<br>
<cfif arrayLen(subgrp_a4c_search_array) GT 0>
    <div>Element subgrp_a4c exists.</div>
    <cfset subgrp_a4c_content = subgrp_a4c_search_array[1].XMLText>
    <div>subgrp_a4c_content: <cfoutput>#subgrp_a4c_content#</cfoutput></div>
<cfelse>
    <div>Element subgrp_a4c doesn't exist.</div>
</cfif>

Votes

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
Engaged ,
Aug 24, 2009 Aug 24, 2009

Copy link to clipboard

Copied

LATEST

I do not profess to be an XPath guru, but it seems to me that you should be able to find a more suitable XPath-expression than this one...

Votes

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
Resources
Documentation