Jump to content
AndrewHoward

Validating an XML using XSD

Recommended Posts

Hello,

Does anyone have an example on how to validate an XML using an XSD file in Delphi ?

 

I would like to get data from database & build an XML which needs to be valid.

Thank you.

Share this post


Link to post
procedure TImportExport.CheckXMLByXSD(xml_file, xsd_file: string; out FileName: string);
var
  FXMLDocument: IXMLDOMDocument2;
  FXMLDOMSchema: IXMLDOMSchemaCollection2;
  FXMLParserError: IXMLDOMParseError2;
  s: string;
  i: integer;
  flk: IXMLFLK_PType;
begin
  FileName:= '';
  try
    FXMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0') as IXMLDomDocument2;
    FXMLDOMSchema := CreateOleObject('Msxml2.XMLSchemaCache.6.0') as IXMLDOMSchemaCollection2;

    FXMLDOMSchema.add('', xsd_file);
    FXMLDocument.Async := false;
    FXMLDocument.resolveExternals:= false;
    FXMLDocument.validateOnParse := false;
    FXMLDocument.setProperty('MultipleErrorMessages', true);
    FXMLDocument.load(xml_file);
    FXMLDocument.schemas := FXMLDOMSchema;
    FXMLParserError := FXMLDocument.validate as IXMLDOMParseError2;
  finally
    if (FXMLParserError.errorCode <> 0) then
      begin
        s:= '';
        flk:= NewFLK_P;
        flk.FNAME:= ReplaceFirstChar(ExtractFileName(xml_file),'V');
        flk.FNAME_I:= ExtractFileName(xml_file);
        with FXMLParserError.allErrors do
          for i:= 0 to Length - 1 do
            begin
              with flk.PR.Add do
                begin
                  case Item[i].ErrorCode of
                    -1072897535: OSHIB:= 903;
                    -1072898028: OSHIB:= 902;
                  end;
                  BAS_EL:= Item[i].errorXPath;
                  COMMENT:= StringReplace(Item[i].reason, #13#10, '', [rfReplaceAll]);
                end;
              s:= s + Format('ErrorCode: %d' + #13#10 + 'Reason: %s' + #13#10 +
                             'SrcText: %s' + #13#10 + 'Line: %d' + #13#10 +
                             'LinePos: %d' + #13#10 + 'FilePos: %d' + #13#10 +
                             'XPath: %s', [Item[i].ErrorCode, Item[i].reason, Item[i].Srctext, Item[i].Line, Item[i].LinePos, Item[i].FilePos, Item[i].errorXPath])+ #13#10;
            end;
        FileName:= TempFolder + ReplaceFirstChar(ExtractFileName(xml_file),'V');
        flk.OwnerDocument.LoadFromXML(XMLDoc.FormatXMLData(flk.OwnerDocument.XML.Text));
        flk.OwnerDocument.SaveToFile(FileName);
        raise Exception.Create('Файл ' + ExtractFileName(xml_file) +
                               ' не соответствует своей XSD схеме ' + ExtractFileName(xsd_file) + #13#10 + s);
      end;
    FXMLParserError:= nil;
    FXMLDOMSchema:= nil;
    FXMLDocument:= nil;
  end;
end;

from project 🙂

  • Thanks 1

Share this post


Link to post

That's very helpful.  Thank you very much ZDeveloper.

Could you tell me what's the defenition of this type:

IXMLFLK_PType

Thanks again.

Share this post


Link to post

its my own class for generating output xml about errors in source file

you can take only this part 

try
	FXMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0') as IXMLDomDocument2;
    FXMLDOMSchema := CreateOleObject('Msxml2.XMLSchemaCache.6.0') as IXMLDOMSchemaCollection2;

    FXMLDOMSchema.add('', xsd_file);
    FXMLDocument.Async := false;
    FXMLDocument.resolveExternals:= false;
    FXMLDocument.validateOnParse := false;
    FXMLDocument.setProperty('MultipleErrorMessages', true);
    FXMLDocument.load(xml_file);
    FXMLDocument.schemas := FXMLDOMSchema;
    FXMLParserError := FXMLDocument.validate as IXMLDOMParseError2;

and part for error check

  finally
    if (FXMLParserError.errorCode <> 0) then
      begin

//////////////////////////////////////////////////

        with FXMLParserError.allErrors do
          for i:= 0 to Length - 1 do

//// here you can log errors or doing somethings else

 

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×