加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在delphi中使用msxml进行模式验证

发布时间:2020-12-15 10:07:43 所属栏目:大数据 来源:网络整理
导读:我正在尝试根据它引用的模式验证XML文件. (使用Delphi和MSXML2_TLB.)代码的(相关部分)看起来像这样: procedure TfrmMain.ValidateXMLFile;var xml: IXMLDOMDocument2; err: IXMLDOMParseError; schemas: IXMLDOMSchemaCollection;begin xml := ComsDOMDocum
我正在尝试根据它引用的模式验证XML文件. (使用Delphi和MSXML2_TLB.)代码的(相关部分)看起来像这样:
procedure TfrmMain.ValidateXMLFile;
var
    xml: IXMLDOMDocument2;
    err: IXMLDOMParseError;
    schemas: IXMLDOMSchemaCollection;
begin
    xml := ComsDOMDocument.Create;
    if xml.load('Data/file.xml') then
    begin
        schemas := xml.namespaces;
        if schemas.length > 0 then
        begin
            xml.schemas := schemas;
            err := xml.validate;
        end;
    end;
end;

这导致加载缓存(schemas.length> 0),但是下一个赋值会引发异常:“只能使用XMLSchemaCache-schemacollections.”

我该怎么办呢?

解决方法

我想出了一种似乎有用的方法.我首先显式加载模式,然后将它们添加到schemacollection.接下来,我加载xml文件并将schemacollection分配给其schemas属性.解决方案现在看起来像这样:
uses MSXML2_TLB  
That is:  
// Type Lib: C:Windowssystem32msxml4.dll  
// LIBID: {F5078F18-C551-11D3-89B9-0000F81FE221}

function TfrmMain.ValidXML(
    const xmlFile: String; 
    out err: IXMLDOMParseError): Boolean;
var
    xml,xsd: IXMLDOMDocument2;
    cache: IXMLDOMSchemaCollection;
begin
    xsd := CoDOMDocument40.Create;
    xsd.Async := False;
    xsd.load('http://the.uri.com/schemalocation/schema.xsd');

    cache := CoXMLSchemaCache40.Create;
    cache.add('http://the.uri.com/schemalocation',xsd);

    xml := CoDOMDocument40.Create;
    xml.async := False;
    xml.schemas := cache;

    Result := xml.load(xmlFile);
    if not Result then
      err := xml.parseError
    else
      err := nil;
end;

使用XMLSchemaCache40或更高版本非常重要.早期版本不遵循W3C XML Schema标准,而只是针对MicroSoft规范XDR Schema进行验证.

这个解决方案的缺点是我需要显式加载模式.在我看来,应该可以自动检索它们.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读