如何使用delphi访问msxml6中的complextype中的粒子?

时间:2011-09-20 00:53:09

标签: delphi delphi-7 msxml

当访问contentmodel中的partilces时,我得到'参数不正确'但是可以读取itemtype。谁能告诉我该怎么做?提前谢谢。

//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" />

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;

端;

1 个答案:

答案 0 :(得分:2)

问题不在您的代码中,问题在于错误的Delphi 7 TLB导入程序。

除了您忘记包含xs:schema结束标记这一事实外,如果我将其复制粘贴到 Delphi 2010中,那么您的示例就可以了 EM>

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

快速查看生成的TLB.pas表明Delphi 7在导入.TLB文件时搞砸了。 contentModel属于ISchemaModelGroup类型,继承自ISchemaItem。现在来看看定义:

  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方法? Delphi 2010不会生成这些,并且它们不应该首先存在(它们会导致get_particles调用的方法偏移完全错误)。

只需在MSXML2_TLB中注释那些GhostMethod_XXX方法,您的示例就像魅力一样。

然而,你仍然坚持使用严重导入的TLB,这可能随时在你的脸上爆炸。我建议您使用Delphi 2010导入版本(MSXML2_TLB.zip),因为它适用于Delphi 7。