[Lazarus] TDOMParseOptions, TDOMParser, TXMLDocument, ... how to set TDOMParser Options?

Sergei Gorelkin sergei_gorelkin at mail.ru
Fri Nov 27 04:55:24 CET 2009


Michael Joyner ᏩᏯ пишет:
> TDOMParseOptions, TDOMParser, TXMLDocument, ... how to set TDOMParser 
> Options?
> 
> I am trying to the use the xmlread and xmlwrite units.
> 
> I would like to set the DOM Parser options, I can see in 
> 'fpcsrc/2.2.4/packages/fcl-xml/src'  they are defined in the class by 
> another class called TDOMParserOptions, but they are read only in the 
> TDOMParser Class?

You don't need to create an instance of TDOMParseOptions. Every TDOMParser already has one 
available. Just create a TDOMParser and set its options like this:

domParser := TDOMParser.Create;
domParser.Options.ExpandEntities := True;

See e.g. the "Validating a document" example in http://wiki.freepascal.org/XML_Tutorial

> Sample code that won't work do to the read only option for the options:
> 
> procedure TForm1.Button1Click(Sender: TObject);
> begin
>  if OpenDialog.Execute then begin
>     domOptions := TDOMParseOptions.Create;
>     domOptions.ExpandEntities:=true;
>     domParser := TDOMParser.Create;
>     domParser.Options := domOptions;
>     xdoc := TXMLDocument.create;
>     domParser.ParseURI(OpenDialog.Filename,xdoc);
>  end;
> end;    

Note that you don't have to create a TXMLDocument before parsing, because parser always creates one. 
Also, using filename directly as an URI won't work on e.g. Windows; use FilenameToURI function from 
URIParser unit to convert it in a crossplatform way.

Regards,
Sergei




More information about the Lazarus mailing list