0

Is there a way for Lazarus Free Pascal to check if a XML Code is well formed without using a DTD?

A DTD is really specific but i just want to check if the Syntax is right.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    Well, any XML parser you use with Lazarus/FPC should tell you whether a document is well-formed simply by attempting to load it. – MartynA Jul 28 '20 at 08:35
  • Is "well formed" a synonym to "valid" to you? Then see https://stackoverflow.com/a/134498/4299358 – AmigoJack Jul 28 '20 at 12:05
  • Any compliant XML parser will report well-formedness problems without a DTD (or schema of any type). Validation against a DTD (or schema) is a further check. See duplicate link for further details. – kjhughes Jul 28 '20 at 17:05

1 Answers1

0

Thank you for your help, i found something here https://wiki.lazarus.freepascal.org/XML_Tutorial/de but i wasnt sure if i need a DTD.

uses  XMLRead,DOM;
procedure TXML.XMLValidate(input : string);           
//https://wiki.lazarus.freepascal.org/XML_Tutorial/de
  var 
    Parser: TDOMParser;                         
    Src: TXMLInputSource;
    TheDoc: TXMLDocument;
  begin
    // Create new ParseObject
    Parser := TDOMParser.Create;
    try
      // Load the XML in a TXMLInputSource
      Src := TXMLInputSource.Create(input);
      try
        // Check structure
        Parser.Options.Validate := True;
        // You can call a Method for an Error if needed
        //Parser.OnError := ErrorHandler;
        // This is the check, we get an exception if the Syntax is not true
        try
          Parser.Parse(Src, TheDoc);
          Error := false;
        except
          Error := true;
        end;
      // ... and clean
      finally
        Src.Free;
      end;
    finally
      Parser.Free;
    end;
  end;    

 
HeartWare
  • 7,464
  • 2
  • 26
  • 30