2011-09-20 43 views
2

访问contentmodel中的partilces时,我得到的'参数不正确',但可以读取itemtype。有人能告诉我该怎么做吗?提前致谢。如何使用delphi在msxml6中访问complextype中的粒子?

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

端;

回答

2

问题不在于你的代码中,问题在于有故障的Delphi 7 TLB导入程序。

除了事实,你忘了包括xs:schema结束标记,你的榜样工程就好如果我把它复制粘贴到德尔福2010

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

快速查看生成的TLB.pas,建议在导入.TLB文件时搞砸了Delphi 7。 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方法?德尔福2010年不会生成这些,他们不应该在那里(他们导致get_particles调用的方法偏移全部错误)。

只需评论MSXML2_TLB中的那些GhostMethod_XXX方法,您的示例就像一个魅力。

但是,你仍然陷入了一个严重导入的TLB,它可能随时炸毁你的脸。我建议你使用Delphi 2010导入版本代替(MSXML2_TLB.zip),因为它可以与Delphi 7一起工作。