Replace special characters in XML - Blog - SAN Business Solutions

Search
Go to content

Main menu:

Replace special characters in XML

Published by in MS Dynamics NAV · 10/2/2017 13:35:00
When you create a XML file from MS Dynamics NAV you need often replace some special characters in text.
Some characters are in general not allowed to use (&<), while others are permitted with DTD.
Here, the example of the five special symbols shows how to replace the characters by strings.
You can use the function ReplaceText() where the first parameter is a text in, and the second - the text out.

VAR
CharOption@1000000003 : ''',<,>,&,"';
ReplaceOption@1000000002 : '&apos;,&lt;,&gt;,&amp;,&quot;';
OptionMax@1000000004 : Integer;

PROCEDURE ReplaceText@1000000001(XmlText@1000000000 : Text[250]) XmlTextNew : Text[250];
VAR
CharText@1000000001 : Text[30];
I@1000000002 : Integer;
BEGIN
CharText := OptionToText();
OptionMax := SubStrNo(CharText,',');
FOR I := 1 TO STRLEN(XmlText) DO
XmlTextNew += ReplaceChar(FORMAT(XmlText[I]));
END;

PROCEDURE ReplaceChar@1000000000(CurrCharText@1000000000 : Text[5]) : Text[30];
VAR
I@1000000001 : Integer;
BEGIN
FOR I := 0 TO OptionMax DO BEGIN
CharOption := I;
ReplaceOption := I;
IF CurrCharText = FORMAT(CharOption) THEN
EXIT(FORMAT(ReplaceOption));
END;
EXIT(CurrCharText);
END;

PROCEDURE OptionToText@1000000011() Text : Text[250];
VAR
PartStr@1000000001 : Text[250];
I@1000000000 : Integer;
BEGIN
FOR I := 0 TO 10 DO BEGIN
CharOption := I;
PartStr := FORMAT(CharOption);
IF PartStr = FORMAT(I) THEN
PartStr := '';
IF PartStr <> '' THEN BEGIN
IF Text = '' THEN
Text := PartStr
ELSE
Text := Text + ',' + PartStr;
END;
END;
END;

PROCEDURE SubStrNo@1000000020(Str@1000000000 : Text[1000];SubStr@1000000001 : Text[50]) : Integer;
VAR
No@1000000003 : Integer;
Pos@1000000002 : Integer;
BEGIN
Pos := STRPOS(Str,SubStr);
WHILE Pos > 0 DO BEGIN
No += 1;
Str := DELSTR(Str,Pos,STRLEN(SubStr));
Pos := STRPOS(Str,SubStr);
END;
EXIT(No)
END;


Back to content | Back to main menu