[yunqa.de] HTMLParser - attribute escaping for xml

  • From: Michael Schwarzl <schwarzl@xxxxxxx>
  • To: yunqa@xxxxxxxxxxxxx
  • Date: Fri, 1 Apr 2016 11:02:05 +0200

Hello,

I'm using the TDIHtmlWriterPlugin for creating my xml-files. When I add 
attributes with values containing special chars like < or '  the escaping does 
not math the xml standard.
Here I do need some help on how I can create correct escaped attributes.

This is a small example to repdroduce the xml:

var
  tmpPlugin: TDIHtmlWriterPlugin;
  tmpTag: TDIHtmlTag;
  tmpMem: TMemoryStream;
begin
  RegisterTag('anytag', 2000);
  RegisterAttrib('myattr', 2000);

  tmpPlugin := TDIHtmlWriterPlugin.Create(nil);
  tmpPlugin.SetAllFilters(fiShow);
  tmpPlugin.QuoteHtmlTags := qtAlways;
  tmpPlugin.QuoteCustomTags := qtAlways;
  tmpPlugin.EmptyCustomAttribValues := True;
  tmpPlugin.EmptyHtmlAttribValues := True;

  tmpPlugin.PredefinedEntities := DEFAULT_PREDEFINED_ENTITIES;

  tmpPlugin.Writer.WriteMethods := UnicodeEncodings[1].Write;  // UTF-8 
encoding
  tmpPlugin.Writer.WriteStrW('<?xml version="1.0" encoding="UTF-8"?>');
  tmpPlugin.Writer.WriteLineBreak;

  tmpTag := TDIHtmlTag.Create(GetDIUnicodeString2CardinalItemHandler);
  tmpTag.TagName := 'anytag';
  tmpTag.TagType := ttEmptyElementTag;
  tmpTag.ForceAttrib(2000, 'myattr', '<%VARIABLE%>');

  tmpPlugin.WriteHtmlTag(tmpTag);

  FreeAndNil(tmpTag);

  tmpMem := TMemoryStream.Create;
  tmpMem.Write(BOM_UTF_8, Length(BOM_UTF_8));    // our export classes just 
create UTF-8 encoded XMLs
  tmpPlugin.Writer.SaveDataToStream(tmpMem);


The result is:
<?xml version="1.0" encoding="UTF-8"?>
<anytag myattr="<%VARIABLE%>"/>
The < and > within the attribute should be escaped.

Any help is apreciated.

Best regards,
Michael


Other related posts: