Recent

Author Topic: XML special chars convert [SOLVED by Leledumbo and wp]  (Read 525 times)

totya

  • Hero Member
  • *****
  • Posts: 577
XML special chars convert [SOLVED by Leledumbo and wp]
« on: August 13, 2019, 09:13:02 pm »
Hi!

I have a lines from a xml (I read it full manually because I need it), for example (code):

A < < B

This is mean (decode):

A < < B

Is there any built-in function in fpc/Lazarus to convert all exists xml codes (code and decode) from string to string?

Thanks!
« Last Edit: August 13, 2019, 09:46:00 pm by totya »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8114
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: XML special chars convert
« Reply #1 on: August 13, 2019, 09:34:51 pm »
htmlelements unit from fcl-xml has both EscapeHTML and UnescapeHTML functions.

totya

  • Hero Member
  • *****
  • Posts: 577
Re: XML special chars convert
« Reply #2 on: August 13, 2019, 09:40:12 pm »
htmlelements unit from fcl-xml has both EscapeHTML and UnescapeHTML functions.

Seems to me its working, thank you!

wp

  • Hero Member
  • *****
  • Posts: 6485
Re: XML special chars convert [SOLVED by Leledumbo]
« Reply #3 on: August 13, 2019, 09:44:14 pm »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

totya

  • Hero Member
  • *****
  • Posts: 577

totya

  • Hero Member
  • *****
  • Posts: 577
Re: XML special chars convert [SOLVED by Leledumbo and wp]
« Reply #5 on: August 25, 2019, 10:39:58 pm »
These two method works similar, but I don't like the result. I recreate the xml codes that way - see second line. (Insert code result garbage s variable, so I used insert quote)

Quote
procedure TForm1.Button1Click(Sender: TObject);
const
  s = 'AA &#10; BB';
begin
  Memo1.Lines.Add(XMLValueToStr(s));
  Memo1.Lines.Add(StrToXMLValue(XMLValueToStr(s)));
  //Memo1.Lines.Add(XMLValueToStr(StrToXMLValue(XMLValueToStr(s))));
  Memo1.Lines.Add('');
  Memo1.Lines.Add(UnEscapeHTML(s));
  Memo1.Lines.Add(EscapeHTML(UnEscapeHTML(s)));
  //Memo1.Lines.Add(UnEscapeHTML(EscapeHTML(UnEscapeHTML(s))));
end;

I got:
Quote
AA &#10; BB
AA &amp;#10; BB

AA &#10; BB
AA &amp;#10; BB

But I'd like to keep "AA &#10; BB"...

Edit:
This seems to me ugly code:
Quote
StringReplace(Value, '&amp;#', '&#', [rfReplaceAll]);
« Last Edit: August 25, 2019, 10:53:01 pm by totya »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8114
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: XML special chars convert [SOLVED by Leledumbo and wp]
« Reply #6 on: August 25, 2019, 11:51:51 pm »
But I'd like to keep "AA &#10; BB"...
Then you're not fully escaping as it's meant to be. Come to think of it, what would happen if you unescape with the incompletely escaped value? Consistency is there for a reason.
This seems to me ugly code:
It's ugly from the start: why would you ever want to do that?

totya

  • Hero Member
  • *****
  • Posts: 577
Re: XML special chars convert [SOLVED by Leledumbo and wp]
« Reply #7 on: August 26, 2019, 06:34:52 am »
Because this is the original xml code, and this is okay. I want only add the missing xml charcodes to the string... Shortly, this function (from the laz2_dom) works incorrectly in this situation. But with my ugly code, this will okay.