如题
最好能报错

解决方案 »

  1.   

    我也正需要这方面的知识,我找到有些代码,但奇怪的是不能正常使用
    procedure TFrmPrintReport.Button2Click(Sender: TObject);
    var
      iDomDoc40: DOMDocument40;
      iSchemaDoc: DOMDocument40;
      iDomParseError: IXMLDOMParseError ;
      iSchemaCache: XMLSchemaCache40;
      sNameSpace: string;
     // iXMLSchema: ISchema;
    begin
      iSchemaDoc :=CoDOMDocument40.Create;
      iSchemaDoc.async := False;
      iSchemaDoc.validateOnParse := False;
      iSchemaDoc.load(ExtractFilePath(Application.Exename) +'tests\manifest.xsd');  //得到命名空间
      sNameSpace := iSchemaDoc.documentElement.getAttributeNode('xmlns:xs').Value;  iSchemaCache := CoXMLSchemaCache40.Create;
      iSchemaCache.add(sNameSpace, iSchemaDoc);
      //iXMLSchema :=iSchemaCache.getSchema(sNameSpace);  iDomDoc40 :=CoDOMDocument40.Create;
      iDomDoc40.async :=False;  //主要是下面这两行代码
      iDomDoc40.validateOnParse := True;
      iDomDoc40.schemas := iSchemaCache;
      iDomDoc40.load('D:\RONGXING\旅客登记II\ReportAir\2007-11-20\SA_0711200845_ZYK.xml');
      iDomParseError := iDomDoc40.parseError;
      if iDomParseError.errorCode = 0 then
        ShowMessage('验证成功');  //如果验证失败,还可以通过下面的信息来定位错误的地方
      //iDomParseError.reason;
      //iDomParseError.srcText;
      //iDomParseError.line;
    end;
      

  2.   

    另一种,也同样无效procedure TFrmPrintReport.Button1Click(Sender: TObject);
    var
       XDoc              : IXMLDomDocument2;
       XSchemaCache      : IXMLDomSchemaCollection2;
       errortxt          : string;
       fs                : TfileStream;
       data              : string;
       SchemaFile        : WideString;
    begin
       XDoc := CreateDOMDocument as IXMLDomDocument2;   { instancieer SchemaCache }
       XSchemaCache := CoXMLSchemaCache40.Create;
       try
         try
           SchemaFile := ExtractFilePath(Application.Exename) +'tests\manifest.xsd';
           XSchemaCache.Add('xsi:noNamespaceSchemaLocation', SchemaFile);
         except
           on E: Exception do
           begin
             ShowMessageFmt('SchemaCache.add <%s>'#13#10 + 'Error: %s',[SchemaFile, E.Message]);
             exit;
           end;
         end;
         try
           XDoc.Schemas := XSchemaCache;
           XDoc.async := False;
           XDoc.resolveExternals := True;
           XDoc.validateOnParse := True;
           
           fs := TFileStream.create('D:\RONGXING\旅客登记II\ReportAir\2007-11-20\SA_0711200845_ZYK.xml', fmOpenRead or fmShareDenyWrite);
           try
             SetLength(data, fs.Size);
             fs.Read(data[1], fs.Size);
           finally
             fs.Free;
           end;       if not XDoc.loadXML(WideString(data)) then
           begin
             errortxt := Format('IXMLDomDocument40 error.'#13#10 +
               'Reason: %s'#13#10 +
               'Source: %s',
               [XDoc.parseError.reason, XDoc.parseError.srcText]);
             ShowMessage(errortxt);
             exit;
           end;
         except
           on E: Exception do
           begin
             errortxt := Format('IXMLDomDocument40 error: %s'#13#10 +
               'Reason: %s'#13#10 +
               'Source: %s',
               [E.Message, XDoc.parseError.reason, XDoc.parseError.srcText]);
             ShowMessage(errortxt);
             exit;
           end;
         end;
       finally
         ShowMessage('验证成功');  
         XDoc := nil;
         XSchemaCache := nil;
       end;
    end;
      

  3.   

    找了好久`~~`终于有一个可以的`~~试试var
      SchemaDoc, XmlDoc: IXMLDOMDocument2;
      SchemaCache: IXMLDOMSchemaCollection;
      Error: IXMLDOMParseError;
    begin
        // Load the data
        XmlDoc := CoDOMDocument40.Create;
        XmlDoc.async := False;
        XmlDoc.load('D:\SA_0711200845_ZYK.xml');
        // Load the schema doc
        SchemaDoc := CoDOMDocument40.Create;
        SchemaDoc.async := False;
        SchemaDoc.load(ExtractFilePath(Application.Exename) +'tests\manifest.xsd');
        // Register the schema with the cache
        SchemaCache := CoXMLSchemaCache40.Create;
        SchemaCache.add('', schemadoc);
        // Assign the schema cache to the document
        XmlDoc.schemas := schemacache;
        // Validate and report
        Error := xmldoc.validate;
        if Error.errorCode <> S_OK then
          Memo1.Lines.Add(Error.reason)
        else
          Memo1.Lines.Add('验证成功');  
    end;