Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling special characters in XML string

Status
Not open for further replies.

pankajv

Programmer
Jan 30, 2002
178
0
0
IN
We are building a XML string in VB. Is there any function available which can be used to handle the special characters like "&", " ' " etc. so that the program does not fail when the string is loaded in the parser.
 
Can you post what your final XML is going to look like so that I can possibly make some suggestions?
 
My XML will look like:
<XROADS><ITEM Desc='Name & Description' Date='01/01/2001' Comment='Percentage growth is < 20%'/></XROADS>
 
pankajv,

The Function I use is:-

Public Function XMLFormat(sFieldData As Variant) As Variant
'----------------------------------------------------------
' Author: Codefish
'
' Date: 15/8/2001
'
' History:
'
' Purpose: - Replaces Illegal Characters for their
' XML Equivalent (eg &quot;&&quot;)
'
'----------------------------------------------------------

XMLFormat = sFieldData

If Len(XMLFormat) > 0 Then
'Replace Illegal Chars
XMLFormat= Replace(XMLFormat, &quot;&&quot;, &quot;&amp;&quot;)
XMLFormat= Replace(XMLFormat, &quot;<&quot;, &quot;&lt;&quot;)
XMLFormat= Replace(XMLFormat, &quot;>&quot;, &quot;&gt;&quot;)
XMLFormat= Replace(XMLFormat, &quot;'&quot;, &quot;&apos;&quot;)
XMLFormat= Replace(XMLFormat, Chr(34), &quot;&quot;&quot;)

'Ilegal Char in I.E. 6
XMLFormat= Replace(XMLFormat, &quot;£&quot;, &quot;&163;&quot;)
Else
XMLFormat= &quot;&quot;
End If

End Function

Regards,

Codefish
 
Oops,

forgot that the Browser will render the HTML replacements.

The Replace lines should use the following:-

amp, lt, gt, apos, quot respectively.


Codefish

 
you can (only if you like) use the MSXML parser to build your string

it would take care of converting special characters for you

Code:
Option Explicit

Private Sub Command1_Click()
  Dim oDOM As DOMDocument40
  Dim oElem As IXMLDOMElement
  
  Set oDOM = New DOMDocument40
  
  oDOM.appendChild oDOM.createElement(&quot;XROADS&quot;)
  Set oElem = oDOM.createElement(&quot;ITEM&quot;)
  oDOM.documentElement.appendChild oElem
  oElem.setAttribute &quot;Desc&quot;, &quot;Name & Description&quot;
  oElem.setAttribute &quot;Date&quot;, &quot;01/01/2001&quot;
  oElem.setAttribute &quot;Comment&quot;, &quot;Percentage growth is < 20%&quot;
  Set oElem = Nothing
  
  Text1.Text = oDOM.xml
  
  Set oDOM = Nothing
End Sub

' this gives me the following in the Text Box:
'
'<XROADS><ITEM Desc=&quot;Name &amp; Description&quot; Date=&quot;01/01/2001&quot; Comment=&quot;Percentage growth is &lt; 20%&quot;/></XROADS>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top