unit XMLValidate;
// Requirements ----------------------------------------------------------------
//
// MSXML 4.0 Service Pack 1
// http://www.microsoft.com/downloads/release.asp?releaseid=37176
//
// -----------------------------------------------------------------------------
interface
uses
SysUtils;
type
EValidateXMLError =
class(
Exception)
private
FErrorCode: Integer;
FerrorLine: Integer;
FReason:
string;
public
constructor Create(AErrorCode: Integer;
const Aline: Integer;
const AReason:
string);
property ErrorCode: Integer
read FErrorCode;
property Reason:
string read FReason;
end;
procedure ValidateXMLDoc(
const XmlTestFile, SchemaLocation, SchemaNS: WideString);
overload;
implementation
uses
Windows,
ComObj,
MSXML2_TLB;
resourcestring
RsValidateError = '
Validate XML Error (%.8x), Line (%D), Reason: %s';
{ EValidateXMLError }
constructor EValidateXMLError.Create(AErrorCode: Integer;
const Aline: Integer;
const AReason:
string);
begin
inherited CreateResFmt(@RsValidateError, [AErrorCode, Aline, AReason]);
FErrorCode := AErrorCode;
FReason := AReason;
FerrorLine := Aline;
end;
procedure ValidateXMLDoc(
const XmlTestFile, SchemaLocation, SchemaNS: WideString);
var
xml, xsd: IXMLDOMDocument2;
cache: IXMLDOMSchemaCollection;
res: boolean;
err: IXMLDOMParseError;
begin
xsd := CoDOMDocument40.Create;
xsd.async := False;
xsd.load(SchemaLocation);
cache := CoXMLSchemaCache40.Create;
cache.add(SchemaNS, xsd);
xml := CoDOMDocument40.Create;
xml.async := False;
xml.schemas := cache;
err :=
nil;
res :=
xml.load(XmlTestFile);
if not res
then
begin
err :=
xml.parseError;
if err <>
nil then
raise EValidateXMLError.Create(err.ErrorCode, err.line, err.Reason);
end;
end;
end.