B4A Class EscapeXml - Code snippet

The following code escapes the five XML entities. If you are using a library such as XmlBuilder then it is not required to escape the text as the XML writer will do it automatically.

However if you use a template for the XML file and need to fill some of the fields then this code can be helpful.

B4X:
Public Sub EscapeXml(Raw As String) As String
   Dim sb As StringBuilder
   sb.Initialize
   For i = 0 To Raw.Length - 1
     Dim c As Char = Raw.CharAt(i)
     Select c
       Case QUOTE
         sb.Append(""")
       Case "'"
         sb.Append("'")
       Case "<"
         sb.Append("&lt;")
       Case ">"
         sb.Append("&gt;")
       Case "&"
         sb.Append("&amp;")
       Case Else
         sb.Append(c)
     End Select
   Next
   Return sb.ToString
End Sub
 
Top