Skip to main content
February 13, 2009
Question

getting rid of xml formatting, just returning the text in an element

  • February 13, 2009
  • 2 replies
  • 269 views
In my code below I need to compare the equality of two strings, but I cant seem to get rid of the xml encoding. What method do I use to strip just the xmlText from the xml object. ToString doesn't seem to work. When i finally compare them, that does not make sense either, because CF is returning -1 which is described in the docuemntation to mean that string1 (70 chars) is smaller than string2 (12 chars). Whats going on?
thanks.

<cfif fileExists(#xmlFile#)>
<cfset users = XmlParse(xmlFile)>
<cfloop index="allUsers" array="#XmlSearch( users, '/company/user/username')#">
<cfset jed = "JedSchneider">
<p>
<cfoutput>#jed#, The length of the string #Len(jed)#</cfoutput><br>
<!--- returns: "JedSchneider, The length of the string 12" --->

<cfoutput>#allUsers.#, The length of the string #Len(allUsers)#</cfoutput><br>
<!--- returns: "JedSchneider, The length of the string is 72" --->
<!--- The cause of the 72 chars... in raw html you have the following--->
<!--- <?xml version="1.0" encoding="UTF-8"?> <username>JedSchneider</username> --->

<cfset UsernameCompare = Compare(#allUsers#, #jed#)>

<cfoutput>#UsernameCompare#</cfoutput>
<!--- returns: -1, string1 is smaller than string2 --->
</p>
</cfloop>

<cfelse>
<cfoutput>Bad news, #xmlFile# file doesn't exist</cfoutput>
</cfif>
    This topic has been closed for replies.

    2 replies

    February 14, 2009
    @ BKBK
    Thanks again! It would have taken a while for me to figure out the proper syntax of <cfloop> !
    BKBK
    Community Expert
    Community Expert
    February 13, 2009
    <cfset allUsers = XmlSearch(users, '/company/user/username')>
    <cfset jed = "JedSchneider">

    <cfoutput>#jed#, The length of the string #Len(jed)#</cfoutput><br>

    <cfloop index="n" from="1" to="#arrayLen(allUsers)#">
    <p>
    <cfoutput>username: #allUsers.XmlText#, length of string: #Len(allUsers.XmlText)#</cfoutput><br>
    <cfset UsernameCompare = Compare(allUsers.XmlText, jed)>

    Username compare() returns: <cfoutput>#UsernameCompare#</cfoutput>
    </p>
    </cfloop>