gpt4 book ai didi

delphi - XML 数据绑定(bind)向导未正确绑定(bind) XSD

转载 作者:行者123 更新时间:2023-12-03 15:31:28 25 4
gpt4 key购买 nike

实际上,很简单的问题。只是好奇其他人是否认识到这个问题并且也许有一个可行的解决方案......

我有一个简单的 XSD 文件,用于定义我的应用程序使用的配置文件。我需要将此模式绑定(bind)到我的项目,这是使用 XDBW 完成的。在 Delphi 2007 中,该向导只会将 XSD 识别为模式,因此我将为我的模式获得完美的代码。我最近升级到 Delphi XE,我想再次重新创建这段代码,只是为了好玩。我可以使用旧代码,所以没有问题。我只是想看看 D-XE 是否添加了一些改进。不幸的是,D-XE 解释该文件就像解释任何其他文件一样,并生成可以帮助我从代码创建此架构的代码。铮铮! (那是我的头撞到墙上的声音……)我不想要那样!我想要代码来生成架构所描述的 XML!那么,其他用户是否熟悉这个问题?有解决办法吗?

<小时/>

一种解决方案是仅使用 D2007 源文件。但这不是答案,因为我需要专门通过 D-XE 来完成此操作。

<小时/>

另一个解决方案是将 xsd 文件重命名为 .biz 扩展名。但这是一个极其丑陋的黑客......

最佳答案

这确实是Delphi XE 中的一个错误。它现在在 Embarcadero bug 跟踪系统中作为条目 88893

我尝试使用 XML 数据绑定(bind)向导在 Rad Studio XE 中导入下面的 ASX.XSD 文件,它的工作原理就像您所描述的那样:它导入 XSD,就好像它是 XML 而不是 XSD。

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="asx" type="asxType" />
<xsd:complexType name="asxType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" />
<xsd:element maxOccurs="unbounded" name="entry" type="entryType" />
</xsd:sequence>
<xsd:attribute name="version" type="xsd:decimal" />
</xsd:complexType>
<xsd:complexType name="entryType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" />
<xsd:element name="ref" type="refType" />
<xsd:element name="author" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="refType">
<xsd:attribute name="href" type="xsd:string" />
</xsd:complexType>
</xsd:schema>

它让我得到了这个单位:

{*****************************************************************************************************}
{ }
{ XML Data Binding }
{ }
{ Generated on: 10/12/2010 9:41:06 PM }
{ Generated from: C:\develop\codeplex\TFS05\bo\Native\Delphi\Apps\NederlandFM2WMP\doc\ASX.xsd }
{ Settings stored in: C:\develop\codeplex\TFS05\bo\Native\Delphi\Apps\NederlandFM2WMP\doc\ASX.xdb }
{ }
{*****************************************************************************************************}

unit ASX;

interface

uses xmldom, XMLDoc, XMLIntf;

type

{ Forward Decls }

IXMLSchemaType = interface;
IXMLElementType = interface;
IXMLComplexType = interface;
IXMLComplexTypeList = interface;
IXMLSequenceType = interface;
IXMLAttributeType = interface;

{ IXMLSchemaType }

IXMLSchemaType = interface(IXMLNode)
['{F8A90A02-2236-4A50-A278-CFBB8625B36C}']
{ Property Accessors }
function Get_AttributeFormDefault: UnicodeString;
function Get_ElementFormDefault: UnicodeString;
function Get_Version: UnicodeString;
function Get_Element: IXMLElementType;
function Get_ComplexType: IXMLComplexTypeList;
procedure Set_AttributeFormDefault(Value: UnicodeString);
procedure Set_ElementFormDefault(Value: UnicodeString);
procedure Set_Version(Value: UnicodeString);
{ Methods & Properties }
property AttributeFormDefault: UnicodeString read Get_AttributeFormDefault write Set_AttributeFormDefault;
property ElementFormDefault: UnicodeString read Get_ElementFormDefault write Set_ElementFormDefault;
property Version: UnicodeString read Get_Version write Set_Version;
property Element: IXMLElementType read Get_Element;
property ComplexType: IXMLComplexTypeList read Get_ComplexType;
end;

{ IXMLElementType }

IXMLElementType = interface(IXMLNode)
['{9AAD80FD-06BC-4F82-9321-538F05522F83}']
{ Property Accessors }
function Get_Name: UnicodeString;
function Get_Type_: UnicodeString;
function Get_MaxOccurs: UnicodeString;
procedure Set_Name(Value: UnicodeString);
procedure Set_Type_(Value: UnicodeString);
procedure Set_MaxOccurs(Value: UnicodeString);
{ Methods & Properties }
property Name: UnicodeString read Get_Name write Set_Name;
property Type_: UnicodeString read Get_Type_ write Set_Type_;
property MaxOccurs: UnicodeString read Get_MaxOccurs write Set_MaxOccurs;
end;

{ IXMLComplexType }

IXMLComplexType = interface(IXMLNode)
['{9C0618C5-D81F-4BB5-8DED-0BF9AF757ABD}']
{ Property Accessors }
function Get_Name: UnicodeString;
function Get_Sequence: IXMLSequenceType;
function Get_Attribute: IXMLAttributeType;
procedure Set_Name(Value: UnicodeString);
{ Methods & Properties }
property Name: UnicodeString read Get_Name write Set_Name;
property Sequence: IXMLSequenceType read Get_Sequence;
property Attribute: IXMLAttributeType read Get_Attribute;
end;

{ IXMLComplexTypeList }

IXMLComplexTypeList = interface(IXMLNodeCollection)
['{26C4DF98-1DCA-44C8-9D58-9BEB9348FA5B}']
{ Methods & Properties }
function Add: IXMLComplexType;
function Insert(const Index: Integer): IXMLComplexType;

function Get_Item(Index: Integer): IXMLComplexType;
property Items[Index: Integer]: IXMLComplexType read Get_Item; default;
end;

{ IXMLSequenceType }

IXMLSequenceType = interface(IXMLNodeCollection)
['{9289143E-C0E2-462E-AD77-239966EEAEEC}']
{ Property Accessors }
function Get_Element(Index: Integer): IXMLElementType;
{ Methods & Properties }
function Add: IXMLElementType;
function Insert(const Index: Integer): IXMLElementType;
property Element[Index: Integer]: IXMLElementType read Get_Element; default;
end;

{ IXMLAttributeType }

IXMLAttributeType = interface(IXMLNode)
['{409542A8-7FB2-4C90-BE41-C081A0518773}']
{ Property Accessors }
function Get_Name: UnicodeString;
function Get_Type_: UnicodeString;
procedure Set_Name(Value: UnicodeString);
procedure Set_Type_(Value: UnicodeString);
{ Methods & Properties }
property Name: UnicodeString read Get_Name write Set_Name;
property Type_: UnicodeString read Get_Type_ write Set_Type_;
end;

{ Forward Decls }

TXMLSchemaType = class;
TXMLElementType = class;
TXMLComplexType = class;
TXMLComplexTypeList = class;
TXMLSequenceType = class;
TXMLAttributeType = class;

{ TXMLSchemaType }

TXMLSchemaType = class(TXMLNode, IXMLSchemaType)
private
FComplexType: IXMLComplexTypeList;
protected
{ IXMLSchemaType }
function Get_AttributeFormDefault: UnicodeString;
function Get_ElementFormDefault: UnicodeString;
function Get_Version: UnicodeString;
function Get_Element: IXMLElementType;
function Get_ComplexType: IXMLComplexTypeList;
procedure Set_AttributeFormDefault(Value: UnicodeString);
procedure Set_ElementFormDefault(Value: UnicodeString);
procedure Set_Version(Value: UnicodeString);
public
procedure AfterConstruction; override;
end;

{ TXMLElementType }

TXMLElementType = class(TXMLNode, IXMLElementType)
protected
{ IXMLElementType }
function Get_Name: UnicodeString;
function Get_Type_: UnicodeString;
function Get_MaxOccurs: UnicodeString;
procedure Set_Name(Value: UnicodeString);
procedure Set_Type_(Value: UnicodeString);
procedure Set_MaxOccurs(Value: UnicodeString);
end;

{ TXMLComplexType }

TXMLComplexType = class(TXMLNode, IXMLComplexType)
protected
{ IXMLComplexType }
function Get_Name: UnicodeString;
function Get_Sequence: IXMLSequenceType;
function Get_Attribute: IXMLAttributeType;
procedure Set_Name(Value: UnicodeString);
public
procedure AfterConstruction; override;
end;

{ TXMLComplexTypeList }

TXMLComplexTypeList = class(TXMLNodeCollection, IXMLComplexTypeList)
protected
{ IXMLComplexTypeList }
function Add: IXMLComplexType;
function Insert(const Index: Integer): IXMLComplexType;

function Get_Item(Index: Integer): IXMLComplexType;
end;

{ TXMLSequenceType }

TXMLSequenceType = class(TXMLNodeCollection, IXMLSequenceType)
protected
{ IXMLSequenceType }
function Get_Element(Index: Integer): IXMLElementType;
function Add: IXMLElementType;
function Insert(const Index: Integer): IXMLElementType;
public
procedure AfterConstruction; override;
end;

{ TXMLAttributeType }

TXMLAttributeType = class(TXMLNode, IXMLAttributeType)
protected
{ IXMLAttributeType }
function Get_Name: UnicodeString;
function Get_Type_: UnicodeString;
procedure Set_Name(Value: UnicodeString);
procedure Set_Type_(Value: UnicodeString);
end;

{ Global Functions }

function Getschema(Doc: IXMLDocument): IXMLSchemaType;
function Loadschema(const FileName: string): IXMLSchemaType;
function Newschema: IXMLSchemaType;

const
TargetNamespace = 'http://www.w3.org/2001/XMLSchema';

implementation

{ Global Functions }

function Getschema(Doc: IXMLDocument): IXMLSchemaType;
begin
Result := Doc.GetDocBinding('schema', TXMLSchemaType, TargetNamespace) as IXMLSchemaType;
end;

function Loadschema(const FileName: string): IXMLSchemaType;
begin
Result := LoadXMLDocument(FileName).GetDocBinding('schema', TXMLSchemaType, TargetNamespace) as IXMLSchemaType;
end;

function Newschema: IXMLSchemaType;
begin
Result := NewXMLDocument.GetDocBinding('schema', TXMLSchemaType, TargetNamespace) as IXMLSchemaType;
end;

{ TXMLSchemaType }

procedure TXMLSchemaType.AfterConstruction;
begin
RegisterChildNode('element', TXMLElementType);
RegisterChildNode('complexType', TXMLComplexType);
FComplexType := CreateCollection(TXMLComplexTypeList, IXMLComplexType, 'complexType') as IXMLComplexTypeList;
inherited;
end;

function TXMLSchemaType.Get_AttributeFormDefault: UnicodeString;
begin
Result := AttributeNodes['attributeFormDefault'].Text;
end;

procedure TXMLSchemaType.Set_AttributeFormDefault(Value: UnicodeString);
begin
SetAttribute('attributeFormDefault', Value);
end;

function TXMLSchemaType.Get_ElementFormDefault: UnicodeString;
begin
Result := AttributeNodes['elementFormDefault'].Text;
end;

procedure TXMLSchemaType.Set_ElementFormDefault(Value: UnicodeString);
begin
SetAttribute('elementFormDefault', Value);
end;

function TXMLSchemaType.Get_Version: UnicodeString;
begin
Result := AttributeNodes['version'].Text;
end;

procedure TXMLSchemaType.Set_Version(Value: UnicodeString);
begin
SetAttribute('version', Value);
end;

function TXMLSchemaType.Get_Element: IXMLElementType;
begin
Result := ChildNodes['element'] as IXMLElementType;
end;

function TXMLSchemaType.Get_ComplexType: IXMLComplexTypeList;
begin
Result := FComplexType;
end;

{ TXMLElementType }

function TXMLElementType.Get_Name: UnicodeString;
begin
Result := AttributeNodes['name'].Text;
end;

procedure TXMLElementType.Set_Name(Value: UnicodeString);
begin
SetAttribute('name', Value);
end;

function TXMLElementType.Get_Type_: UnicodeString;
begin
Result := AttributeNodes['type'].Text;
end;

procedure TXMLElementType.Set_Type_(Value: UnicodeString);
begin
SetAttribute('type', Value);
end;

function TXMLElementType.Get_MaxOccurs: UnicodeString;
begin
Result := AttributeNodes['maxOccurs'].Text;
end;

procedure TXMLElementType.Set_MaxOccurs(Value: UnicodeString);
begin
SetAttribute('maxOccurs', Value);
end;

{ TXMLComplexType }

procedure TXMLComplexType.AfterConstruction;
begin
RegisterChildNode('sequence', TXMLSequenceType);
RegisterChildNode('attribute', TXMLAttributeType);
inherited;
end;

function TXMLComplexType.Get_Name: UnicodeString;
begin
Result := AttributeNodes['name'].Text;
end;

procedure TXMLComplexType.Set_Name(Value: UnicodeString);
begin
SetAttribute('name', Value);
end;

function TXMLComplexType.Get_Sequence: IXMLSequenceType;
begin
Result := ChildNodes['sequence'] as IXMLSequenceType;
end;

function TXMLComplexType.Get_Attribute: IXMLAttributeType;
begin
Result := ChildNodes['attribute'] as IXMLAttributeType;
end;

{ TXMLComplexTypeList }

function TXMLComplexTypeList.Add: IXMLComplexType;
begin
Result := AddItem(-1) as IXMLComplexType;
end;

function TXMLComplexTypeList.Insert(const Index: Integer): IXMLComplexType;
begin
Result := AddItem(Index) as IXMLComplexType;
end;

function TXMLComplexTypeList.Get_Item(Index: Integer): IXMLComplexType;
begin
Result := List[Index] as IXMLComplexType;
end;

{ TXMLSequenceType }

procedure TXMLSequenceType.AfterConstruction;
begin
RegisterChildNode('element', TXMLElementType);
ItemTag := 'element';
ItemInterface := IXMLElementType;
inherited;
end;

function TXMLSequenceType.Get_Element(Index: Integer): IXMLElementType;
begin
Result := List[Index] as IXMLElementType;
end;

function TXMLSequenceType.Add: IXMLElementType;
begin
Result := AddItem(-1) as IXMLElementType;
end;

function TXMLSequenceType.Insert(const Index: Integer): IXMLElementType;
begin
Result := AddItem(Index) as IXMLElementType;
end;

{ TXMLAttributeType }

function TXMLAttributeType.Get_Name: UnicodeString;
begin
Result := AttributeNodes['name'].Text;
end;

procedure TXMLAttributeType.Set_Name(Value: UnicodeString);
begin
SetAttribute('name', Value);
end;

function TXMLAttributeType.Get_Type_: UnicodeString;
begin
Result := AttributeNodes['type'].Text;
end;

procedure TXMLAttributeType.Set_Type_(Value: UnicodeString);
begin
SetAttribute('type', Value);
end;

end.

但是当使用 .BIZ 扩展名时,它会生成正确的单位:

{*****************************************************************************************************}
{ }
{ XML Data Binding }
{ }
{ Generated on: 10/12/2010 9:47:29 PM }
{ Generated from: C:\develop\codeplex\TFS05\bo\Native\Delphi\Apps\NederlandFM2WMP\doc\ASX.biz }
{ Settings stored in: C:\develop\codeplex\TFS05\bo\Native\Delphi\Apps\NederlandFM2WMP\doc\ASX.xdb }
{ }
{*****************************************************************************************************}

unit ASXUnit;

interface

uses xmldom, XMLDoc, XMLIntf;

type

{ Forward Decls }

IXMLAsxType = interface;
IXMLEntryType = interface;
IXMLEntryTypeList = interface;
IXMLRefType = interface;

{ IXMLAsxType }

IXMLAsxType = interface(IXMLNode)
['{491F413D-0118-47A0-A00E-1BB7FCAD96C2}']
{ Property Accessors }
function Get_Version: UnicodeString;
function Get_Title: UnicodeString;
function Get_Entry: IXMLEntryTypeList;
procedure Set_Version(Value: UnicodeString);
procedure Set_Title(Value: UnicodeString);
{ Methods & Properties }
property Version: UnicodeString read Get_Version write Set_Version;
property Title: UnicodeString read Get_Title write Set_Title;
property Entry: IXMLEntryTypeList read Get_Entry;
end;

{ IXMLEntryType }

IXMLEntryType = interface(IXMLNode)
['{E1F7A8DC-0893-4853-BFFD-19D22AF3313C}']
{ Property Accessors }
function Get_Title: UnicodeString;
function Get_Ref: IXMLRefType;
function Get_Author: UnicodeString;
procedure Set_Title(Value: UnicodeString);
procedure Set_Author(Value: UnicodeString);
{ Methods & Properties }
property Title: UnicodeString read Get_Title write Set_Title;
property Ref: IXMLRefType read Get_Ref;
property Author: UnicodeString read Get_Author write Set_Author;
end;

{ IXMLEntryTypeList }

IXMLEntryTypeList = interface(IXMLNodeCollection)
['{4CA3727D-89DA-4498-B617-48D413D63887}']
{ Methods & Properties }
function Add: IXMLEntryType;
function Insert(const Index: Integer): IXMLEntryType;

function Get_Item(Index: Integer): IXMLEntryType;
property Items[Index: Integer]: IXMLEntryType read Get_Item; default;
end;

{ IXMLRefType }

IXMLRefType = interface(IXMLNode)
['{0860418C-E35A-4315-8B1C-7C0D19C45740}']
{ Property Accessors }
function Get_Href: UnicodeString;
procedure Set_Href(Value: UnicodeString);
{ Methods & Properties }
property Href: UnicodeString read Get_Href write Set_Href;
end;

{ Forward Decls }

TXMLAsxType = class;
TXMLEntryType = class;
TXMLEntryTypeList = class;
TXMLRefType = class;

{ TXMLAsxType }

TXMLAsxType = class(TXMLNode, IXMLAsxType)
private
FEntry: IXMLEntryTypeList;
protected
{ IXMLAsxType }
function Get_Version: UnicodeString;
function Get_Title: UnicodeString;
function Get_Entry: IXMLEntryTypeList;
procedure Set_Version(Value: UnicodeString);
procedure Set_Title(Value: UnicodeString);
public
procedure AfterConstruction; override;
end;

{ TXMLEntryType }

TXMLEntryType = class(TXMLNode, IXMLEntryType)
protected
{ IXMLEntryType }
function Get_Title: UnicodeString;
function Get_Ref: IXMLRefType;
function Get_Author: UnicodeString;
procedure Set_Title(Value: UnicodeString);
procedure Set_Author(Value: UnicodeString);
public
procedure AfterConstruction; override;
end;

{ TXMLEntryTypeList }

TXMLEntryTypeList = class(TXMLNodeCollection, IXMLEntryTypeList)
protected
{ IXMLEntryTypeList }
function Add: IXMLEntryType;
function Insert(const Index: Integer): IXMLEntryType;

function Get_Item(Index: Integer): IXMLEntryType;
end;

{ TXMLRefType }

TXMLRefType = class(TXMLNode, IXMLRefType)
protected
{ IXMLRefType }
function Get_Href: UnicodeString;
procedure Set_Href(Value: UnicodeString);
end;

{ Global Functions }

function Getasx(Doc: IXMLDocument): IXMLAsxType;
function Loadasx(const FileName: string): IXMLAsxType;
function Newasx: IXMLAsxType;

const
TargetNamespace = '';

implementation

{ Global Functions }

function Getasx(Doc: IXMLDocument): IXMLAsxType;
begin
Result := Doc.GetDocBinding('asx', TXMLAsxType, TargetNamespace) as IXMLAsxType;
end;

function Loadasx(const FileName: string): IXMLAsxType;
begin
Result := LoadXMLDocument(FileName).GetDocBinding('asx', TXMLAsxType, TargetNamespace) as IXMLAsxType;
end;

function Newasx: IXMLAsxType;
begin
Result := NewXMLDocument.GetDocBinding('asx', TXMLAsxType, TargetNamespace) as IXMLAsxType;
end;

{ TXMLAsxType }

procedure TXMLAsxType.AfterConstruction;
begin
RegisterChildNode('entry', TXMLEntryType);
FEntry := CreateCollection(TXMLEntryTypeList, IXMLEntryType, 'entry') as IXMLEntryTypeList;
inherited;
end;

function TXMLAsxType.Get_Version: UnicodeString;
begin
Result := AttributeNodes['version'].Text;
end;

procedure TXMLAsxType.Set_Version(Value: UnicodeString);
begin
SetAttribute('version', Value);
end;

function TXMLAsxType.Get_Title: UnicodeString;
begin
Result := ChildNodes['title'].Text;
end;

procedure TXMLAsxType.Set_Title(Value: UnicodeString);
begin
ChildNodes['title'].NodeValue := Value;
end;

function TXMLAsxType.Get_Entry: IXMLEntryTypeList;
begin
Result := FEntry;
end;

{ TXMLEntryType }

procedure TXMLEntryType.AfterConstruction;
begin
RegisterChildNode('ref', TXMLRefType);
inherited;
end;

function TXMLEntryType.Get_Title: UnicodeString;
begin
Result := ChildNodes['title'].Text;
end;

procedure TXMLEntryType.Set_Title(Value: UnicodeString);
begin
ChildNodes['title'].NodeValue := Value;
end;

function TXMLEntryType.Get_Ref: IXMLRefType;
begin
Result := ChildNodes['ref'] as IXMLRefType;
end;

function TXMLEntryType.Get_Author: UnicodeString;
begin
Result := ChildNodes['author'].Text;
end;

procedure TXMLEntryType.Set_Author(Value: UnicodeString);
begin
ChildNodes['author'].NodeValue := Value;
end;

{ TXMLEntryTypeList }

function TXMLEntryTypeList.Add: IXMLEntryType;
begin
Result := AddItem(-1) as IXMLEntryType;
end;

function TXMLEntryTypeList.Insert(const Index: Integer): IXMLEntryType;
begin
Result := AddItem(Index) as IXMLEntryType;
end;

function TXMLEntryTypeList.Get_Item(Index: Integer): IXMLEntryType;
begin
Result := List[Index] as IXMLEntryType;
end;

{ TXMLRefType }

function TXMLRefType.Get_Href: UnicodeString;
begin
Result := AttributeNodes['href'].Text;
end;

procedure TXMLRefType.Set_Href(Value: UnicodeString);
begin
SetAttribute('href', Value);
end;

end.

错误:很快就会进行质量控制。

关于delphi - XML 数据绑定(bind)向导未正确绑定(bind) XSD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3871221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com