Forgot your password?

Home > About Us > Modern Signal Blog >XML Safe Text

XML Safe Text

I've been working on improving the RSS feeds generated by Lighthouse.  One persistent problem is that some RSS readers (IE, for instance) will choke on a lot of special characters, such as those pasted from Microsoft Word.  I have previously put in place code to replace many of those characters, but came across another one today that wasn't replaced.  I knew I needed a better solution.

On CFlib.org, I found a function called xmlFormat2 that smartly avoids maintaining a list of characters to replace, and just replaces all characters not in a list of "good" characters.  That makes sense.  And it works.

I was concerned, though, about the performance of the function, and I thought it could be done better.  Using the REMatch function (introduced in ColdFusion 8), I was able to make the function both simpler and much faster.  My tests so far have been limited, but it has so far handled everything I have thrown at it.  And here it is:

<cffunction name="XmlSafeText" hint="Replaces all characters that would break an xml file." returnType="string" output="false">       
    <cfargument name="txt" hint="String to format" type="string" required="true">
    <cfset var chars = "">
    <cfset var replaced = "">

    <!--- Use XmlFormat function first --->
    <cfset txt = XmlFormat(txt)>
    <!--- Get all other characters to replace. --->
    <cfset chars = REMatch("[^[:ascii:]]",txt)>
    <!--- Loop through characters and do replace. Maintain a list of characters already replaced to avoid duplicate work. --->
    <cfloop index="char" array="#chars#">
        <cfif ListFind(replaced,char) is 0>
            <cfset txt = Replace(txt,char,"&##" & asc(char) & ";","all")>
            <cfset replaced = ListAppend(replaced,char)>
        </cfif>
    </cfloop>

    <cfreturn txt>
</cffunction>

It should be possible to use it as a replacement for the built in XmlFormat function.  Let me know if you run into any problems with it.

Comments

Eric B's Globally Recognized Avatar This solution works great for the fix I needed. We had international characters introduced to an XML feed we use, and they broken when I ran them through XMLParse. The fixed the issue, so thanks!

Posted on April 13, 2011 1:34:50 PM EDT by Eric B

David Hammond's Globally Recognized Avatar Glad to hear it worked for you!

Posted on April 13, 2011 1:53:07 PM EDT by David Hammond

Leave this field empty

Testimonials

  • Modern Signal worked with us to understand our needs and figure out what solution would work best for us. Our Lighthouse CMS is perfectly suited to our website goals. When we later needed to modify the CMS, they again took the time to understand exactly what was  needed and then built that functionality rather than delivering a cookie cutter solution.   

    - Ecosystem Investment Partners

  • We wouldn’t have gotten where we are today without your support over the years.  Modern Signal has always been a great partner to us.

    - Kirk Gillis, Managing Director at Zoom Tanzania

  • I felt as if my company was their only client. They responded to my needs quickly and efficiently despite short turn around time and intense demands.

    - Teaching Strategies, Inc.

  • This was by far the smoothest website redevelopment I have ever experienced. Modern Signal was a wonderful company to work with and we greatly value our working relationship. 

    - National Association of Student Financial Aid Administrators

  • I love working with Modern Signal! Their CMS is very easy to use and they are incredibly responsive to questions or challenges I bring them.

    - NALP

  • Modern Signal understands our business - from future needs to current limitations - so their solutions are always scalable, solid, and service-oriented.

    - National Association of Home Builders

  • Modern Signal significantly enhanced our site to be more efficient and user-friendly. They provide excellent customer service with timely and cost-effective solutions.

    - Center for Medicare Education

  • Modern Signal has been a great partner for us for over the past 10 years.  As our business grew and our needs changed, Modern Signal was able to work with us to adjust our website platform in the ever-changing online world.  Their service and response level has been second to none, and we've been never been happier with our relationship with them.

    - Charm City Run

  • Modern Signal has a professional staff that was very responsive to our needs during all phases - scoping, developing, implementing and maintaining - of our project.  We have been pleased with their ability to deliver quality work on time and on budget. If given the opportunity, I would work with them again.

    - The National Center for Safe Routes to School