Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating XML files against XSD in FireMonkey

I am developing a cross-platform application (for Windows, Mac etc.) and for 32-bit and 64-bit machines. I have lots of XML files which I need to validate against their XSD.

Is there any way of doing this in Delphi XE3?

I have tried DTD validation but I'm always getting a "DTD prohibited" error. I have also tried solutions mentioned on lots of websites to resolve this error, but I've had no success.

Thanks in advance.

Below is the code I've used...

function TForm2.ValidateXML(const xmlFile : TFileName) : boolean;
 var
 xmlDoc: TXMLDocument;
begin
result := false;
  xmlDoc := TXMLDocument.Create(nil) ;
  try
    xmlDoc.ParseOptions := [poResolveExternals, poValidateOnParse];
   try
      xmlDoc.LoadFromFile(xmlFile) ;
     xmlDoc.Active := true; //this will validate
     result := true;
   except
     on EX : EDOMParseError do
     begin
     ShowMessage('Invalid XML: ' + Ex.Message) ;
   end;
   end;
 finally
   xmlDoc := nil;
 end;
end;
like image 779
Padam Avatar asked Nov 02 '22 19:11

Padam


1 Answers

Obviously you need a cross-platform validator.

Maybe Libxml2 (http://xmlsoft.org/) is an option, as it can be used from many programming languages on many platforms.

A open source Pascal wrapper for Libxml2 is avalaible on http://sourceforge.net/projects/libxml2-pas/

like image 107
mjn Avatar answered Nov 15 '22 04:11

mjn