Ok, I took your example and created a (I suppose) complete encode-decode
unit.
Daniele
*********************************************************************************************
unit XmlParserUnit;
interface
uses StrUtils, SysUtils;
const
XmlNames : Array[1..240] of String = ('"', ''', '&', '<',
'>', ' ', '¡', '¢', '£', '¤', '¥',
'¦', '§', '¨',
'©', 'ª', '«',
'¬', '®', '¯', '°', '±', '²', '³',
'´', 'µ', '¶', '·', '¸', '¹', 'º',
'»', '¼', '½',
'¾', '¿', '×', '÷', 'À', 'Á',
'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç',
'È', 'É', 'Ê',
'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ',
'Ò', 'Ó', 'Ô', 'Õ', 'Ö',
'Ø', 'Ù', 'Ú',
'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à',
'á', 'â', 'ã', 'ä', 'å', 'æ',
'ç', 'è', 'é',
'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð',
'ñ', 'ò', 'ó', 'ô', 'õ',
'ö', 'ø', 'ù',
'ú', 'û', 'ü', 'ý', 'þ', 'ÿ',
'∀', '∂', '∃', '∅', '∇', '∈',
'∉', '∋', '∏',
'∑', '−', '∗', '√', '∝', '∞', '∠',
'∧', '∨', '∩', '∪', '∫', '∴', '∼',
'≅', '≈', '≠',
'≡', '≤', '≥', '⊂', '⊃', '⊄', '⊆', '⊇',
'⊕', '⊗', '⊥', '⋅', 'Α', 'Β',
'Γ', 'Δ', 'Ε',
'Ζ', 'Η', 'Θ', 'Ι', 'Κ', 'Λ', 'Μ',
'Ν', 'Ξ', 'Ο', 'Π', 'Ρ', 'Σ',
'Τ', 'Υ', 'Φ',
'Χ', 'Ψ', 'Ω', 'α', 'β', 'γ', 'δ',
'ε', 'ζ', 'η', 'θ', 'ι', 'κ',
'λ', 'μ', 'ν', 'ξ',
'ο', 'π', 'ρ', 'ς', 'σ', 'τ', 'υ',
'φ', 'χ', 'ψ', 'ω', 'ϑ',
'ϒ', 'ϖ', 'Œ',
'œ', 'Š', 'š', 'Ÿ', 'ƒ', 'ˆ', '˜',
' ', ' ', ' ', '‌', '‍',
'‎', '‏', '–',
'—', '‘', '’', '‚', '“', '”', '„',
'†', '‡', '•', '…', '‰',
'′', '″', '‹',
'›', '‾', '€', '™', '←', '↑', '→',
'↓', '↔', '↵', '⌈', '⌉',
'⌊', '⌋', '◊',
'♠', '♣', '♥', '♦');
XmlValues : Array[1..240] of String = ('"', ''', '&', '<',
'>', ' ', '¡', '¢', '£', '¤', '¥',
'¦', '§', '¨',
'©', 'ª', '«',
'¬', '®', '¯', '°', '±', '²', '³',
'´', 'µ', '¶', '·', '¸', '¹', 'º',
'»', '¼', '½',
'¾', '¿', '×', '÷', 'À', 'Á', 'Â',
'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É',
'Ê', 'Ë', 'Ì',
'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó',
'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û',
'Ü', 'Ý', 'Þ',
'ß', 'à', 'á', 'â', 'ã', 'ä', 'å',
'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì',
'í', 'î', 'ï',
'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö',
'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ',
'ÿ', '∀', '∂',
'∃', '∅', '∇', '∈', '∉', '∋', '∏',
'∑', '−', '∗', '√', '∝',
'∞', '∠', '∧',
'∨', '∩', '∪', '∫', '∴', '∼', '≅',
'≈', '≠', '≡', '≤', '≥',
'⊂', '⊃', '⊄',
'⊆', '⊇', '⊕', '⊗', '⊥', '⋅', 'Α',
'Β', 'Γ', 'Δ', 'Ε', 'Ζ', 'Η',
'Θ', 'Ι', 'Κ',
'Λ', 'Μ', 'Ν', 'Ξ', 'Ο', 'Π', 'Ρ',
'Σ', 'Τ', 'Υ', 'Φ', 'Χ', 'Ψ',
'Ω', 'α', 'β',
'γ', 'δ', 'ε', 'ζ', 'η', 'θ', 'ι',
'κ', 'λ', 'μ', 'ν', 'ξ', 'ο',
'π', 'ρ', 'ς',
'σ', 'τ', 'υ', 'φ', 'χ', 'ψ', 'ω',
'ϑ', 'ϒ', 'ϖ', 'Œ', 'œ', 'Š',
'š', 'Ÿ', 'ƒ',
'ˆ', '˜', ' ', ' ', ' ', '‌', '‍',
'‎', '‏', '–', '—', '‘',
'’', '‚', '“',
'”', '„', '†', '‡', '•', '…', '‰',
'′', '″', '‹', '›',
'‾', '€', '™',
'←', '↑', '→', '↓', '↔', '↵', '⌈',
'⌉', '⌊', '⌋', '◊',
'♠', '♣', '♥',
'♦');
function EncodeXml(aText: WideString; EncodeNumbers: Boolean = False):
String;
function DecodeXml(aText: String): String;
implementation
function EncodeXml(aText: WideString; EncodeNumbers: Boolean = False):
String;
var CtrlCharIdx, PrevIdx, CurrIdx: Integer;
EncodedChar: String;
begin
PrevIdx := 1;
Result := '';
CurrIdx := 1;
while CurrIdx <= Length(aText) do
begin
CtrlCharIdx := AnsiIndexStr('&#' + IntToStr(Ord(aText[CurrIdx])) + ';',
XmlValues);
if CtrlCharIdx >= 0 then
begin
if EncodeNumbers then EncodedChar := XmlValues[CtrlCharIdx+1]
else EncodedChar := XmlNames[CtrlCharIdx+1];
Result := Result + Copy(aText, PrevIdx, CurrIdx - PrevIdx) +
EncodedChar;
PrevIdx := CurrIdx + 1;
end;
Inc(CurrIdx);
end;
if Result = '' then Result := aText
else Result := Result + Copy(aText, PrevIdx, CurrIdx - PrevIdx);
end;
function DecodeXml(aText: String): String;
var SemicolonCharIdx, CtrlCharIdx, PrevIdx, CurrIdx: Integer;
EncodingChar, EncodedChar: String;
begin
PrevIdx := 1;
Result := '';
CurrIdx := 1;
while CurrIdx <= Length(aText) do
begin
if aText[CurrIdx]='&' then
begin
SemicolonCharIdx := PosEx(';', aText, CurrIdx);
if SemicolonCharIdx > 0 then
begin
EncodingChar := Copy(aText, CurrIdx, SemicolonCharIdx - CurrIdx +
1);
CtrlCharIdx := AnsiIndexStr(EncodingChar, XmlNames);
if CtrlCharIdx=-1 then CtrlCharIdx := AnsiIndexStr(EncodingChar,
XmlValues);
if CtrlCharIdx >= 0 then
begin
EncodedChar := WideChar(StrToInt(Copy(XmlValues[CtrlCharIdx+1], 3,
Length(XmlValues[CtrlCharIdx+1]) - 3)));
Result := Result + Copy(aText, PrevIdx, CurrIdx - PrevIdx) +
EncodedChar;
PrevIdx := CurrIdx + Length(EncodingChar);
end;
end;
end;
Inc(CurrIdx);
end;
if Result = '' then Result := aText
else Result := Result + Copy(aText, PrevIdx, CurrIdx - PrevIdx);
end;
end.
***********************************************************************************************************************
--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be