AndrewHoward 0 Posted November 12, 2022 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
ZDeveloper 2 Posted November 14, 2022 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 🙂 1 Share this post Link to post
AndrewHoward 0 Posted November 14, 2022 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
ZDeveloper 2 Posted November 16, 2022 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 1 Share this post Link to post