Manage Learn to apply best practices and optimize your operations.

XML encoding a string

The tip contains code that is a LotusScript version of the @URLEncode function.

View member feedback to this tip.

XML strings must be encoded to conform to standards. Unfortunately, in Domino there are no XML encoding functions like @URLEncode.

The code below is a LotusScript version of this function. It encodes the five specific entities into their XML encoded ones. For all characters outside of the basic ACII, set it to encode these with the character code.

 Code: Function XMLEncode (inputStr As String) As String Dim i As Integer Dim outputStr As String Dim char As String For i = 1 To Len(inputStr) char = Mid$(inputStr, i, 1) If Asc(char) > 31 And Asc(char) < 127 Then Select Case char Case "&": outputStr = outputStr & "&ampamp" Case "<": outputStr = outputStr & "&amplt" Case ">": outputStr = outputStr & "&ampgt" Case "'": outputStr = outputStr & "&apos" Case |"|: outputStr = outputStr & "&ampquot" Case Else: outputStr = outputStr & char End Select Else outputStr = outputStr & "&#" & Asc(char) & ";" End If Next XMLEncode = outputStr End Function

MEMBER FEEDBACK TO THIS TIP

While this tip is potentially useful, it is, in it's current form, unusable.

  1. "&" must be converted to "&ampamp"
  2. "<" must be converted to "&amplt"
  3. ">" can be converted to "&ampgt" for strict XML compliance, although I have not found this to be necessary.

—Jack S.

******************************************

I think the code below may be more efficient than that presented in this tip. You don't need to call any function or loop through text character by character, you just use the Replace function wherever you need to correct strings for XML. By the way, Replace is new with R6.

 Dim array1(4) As String Dim array2(4) As String array1(0) = "&" array1(1) = "<" array1(2) = ">" array1(3) = """" array1(4) = "'" array2(0) = "&ampamp" array2(1) = "&amplt" array2(2) = "&ampgt" array2(3) = "&ampquot" array2(4) = "&ampapos" Replace(text, array1, array2)

—Adam H.

******************************************

This code should use the Uni() function instead of the Asc() function, because XML entities are encoded as Unicode values.

—Rich S.

******************************************

Thanks for your comments on my tip.

Jack S.: I think you misread the code, because it already is in there

Adam H.: You're partially right. For large string this might be more efficient, but you will still need to loop the string character by character for the characters outside the basic ascii set

Rich S.: If the inputStr is Unicoded then you do need Uni()'; if you create the string in script you won't need it.

—Ronald Pijpers, tip author

Do you have comments on this tip?Let us know.

This tip was submitted to the SearchDomino.com tip exchange by member Ronald Pijpers. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.



Dig Deeper on LotusScript

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close