首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用delphi在msxml6中访问复杂类型的粒子?

如何用delphi在msxml6中访问复杂类型的粒子?
EN

Stack Overflow用户
提问于 2011-09-20 08:53:09
回答 1查看 393关注 0票数 2

当访问contentmodel中的部分时,我得到了‘该参数不正确’,但读取itemtype是可以的。有人能告诉我该怎么做吗?提前谢谢。

代码语言:javascript
复制
//Book.xsd
<xs:schema xmlns="urn:bookstore-schema" targetNamespace="urn:bookstore-schema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="book" type="booktype" />
 <xs:complexType name="booktype">
   <xs:sequence>
     <xs:element name="author" type="xs:string" />
     <xs:element name="price" type="xs:decimal" />
     <xs:element name="aaa" type="xs:string" />
   </xs:sequence>
</xs:complexType>
<xs:element name="another" type="xs:string" />

代码语言:javascript
复制
procedure AccessSchema;
var oSchemaCache : XMLSchemaCache60;
    oSchema : ISchema;
    nsTarget : string;
    kk : integer;

 procedure AccessComplexType(oComplex : iSchemaItem);
 var ISchComplex : ISchemaComplexType;
 begin
    ISchComplex := oComplex as ISchemaComplexType;

    if (iSchComplex.contentType = SCHEMACONTENTTYPE_MIXED) or
       (iSchComplex.contentType = SCHEMACONTENTTYPE_ELEMENTONLY) then
    begin
       if (iSchComplex.contentModel.ItemType = SOMITEM_CHOICE) or
          (iSchComplex.contentModel.ItemType = SOMITEM_SEQUENCE) then
       begin
         if IschComplex.contentModel.particles.length > 0 then  
         //error : the parameter is incorrect
        begin
           {handling particles }
        end;
      end;
    end;
 end; 

begin
  oSchemaCache := coXMLSchemaCache60.Create;

  nsTarget := 'urn:bookstore-schema';
  oSchemaCache.add(nsTarget,'c:\book.xsd');
  oSchema := oSchemaCache.getSchema(nsTarget);

   for kk := 0 to pred( oschema.types.length) do
   begin
      if (oschema.types.item[kk].itemType = SOMITEM_COMPLEXTYPE ) then
        AccessComplexType(oschema.types.item[kk]);
   end;

结束;

EN

回答 1

Stack Overflow用户

发布于 2011-09-20 18:00:04

问题不在你的代码中,问题出在有问题的Delphi7TLB导入器。

除了您忘记包含xs:schema结束标记之外,如果我将它复制粘贴到Delphi2010中,您的示例就可以正常工作。

回到Delphi7。访问contentModel的.particles属性将返回OLE代码$80004001。

快速浏览一下生成的TLB.pas就会发现,在导入.TLB文件时,Delphi7搞砸了。contentModel的类型为ISchemaModelGroup,它继承自ISchemaItem。现在来看一下它的定义:

代码语言:javascript
复制
  ISchemaParticle = interface(ISchemaItem)
    ['{50EA08B5-DD1B-4664-9A50-C2F40F4BD79A}']
    procedure GhostMethod_ISchemaParticle_0_1; safecall;
    procedure GhostMethod_ISchemaParticle_4_2; safecall;
    procedure GhostMethod_ISchemaParticle_8_3; safecall;
    procedure GhostMethod_ISchemaParticle_12_4; safecall;
    procedure GhostMethod_ISchemaParticle_16_5; safecall;
    procedure GhostMethod_ISchemaParticle_20_6; safecall;
    procedure GhostMethod_ISchemaParticle_24_7; safecall;
    procedure GhostMethod_ISchemaParticle_28_8; safecall;
    procedure GhostMethod_ISchemaParticle_32_9; safecall;
    procedure GhostMethod_ISchemaParticle_36_10; safecall;
    procedure GhostMethod_ISchemaParticle_40_11; safecall;
    procedure GhostMethod_ISchemaParticle_44_12; safecall;
    procedure GhostMethod_ISchemaParticle_48_13; safecall;
    procedure GhostMethod_ISchemaParticle_52_14; safecall;
    function Get_minOccurs: OleVariant; safecall;
    function Get_maxOccurs: OleVariant; safecall;
    property minOccurs: OleVariant read Get_minOccurs;
    property maxOccurs: OleVariant read Get_maxOccurs;
  end;

看到所有这些ghost_xxx方法了吗?Delphi2010不会生成这些,它们本来就不应该存在(它们会导致get_particles调用的方法偏移量都是错误的)。

只需在MSXML2_TLB中注释这些GhostMethod_XXX方法,您的示例就会非常出色。

然而,你仍然被一个糟糕的导入的TLB卡住了,它可能在任何时候在你面前爆炸。我建议您使用Delphi2010导入版本来代替(MSXML2_TLB.zip),因为它在Delphi7上工作得很好。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7478974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档