问题
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;
回答1:
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/
来源:https://stackoverflow.com/questions/17361799/validating-xml-files-against-xsd-in-firemonkey