有如下XML文件:
<?xml version="1.0" encoding="utf-8"?>
<dataSources>
<dataSource name="姓名1" value="A" flag="true" />
<dataSource name="姓名2" value="B" flag="false" />
<dataSource name="姓名3" value="C" flag="false" />
</dataSources>问:如何读取(name,value,flag)的值及修改等,谢谢
<?xml version="1.0" encoding="utf-8"?>
<dataSources>
<dataSource name="姓名1" value="A" flag="true" />
<dataSource name="姓名2" value="B" flag="false" />
<dataSource name="姓名3" value="C" flag="false" />
</dataSources>问:如何读取(name,value,flag)的值及修改等,谢谢
XMLDoc :IXMLDOMDocument;
ClientCardList:IXMLDOMNodeList;
ClientCardNode:IXMLDOMElement;
begin
XMLDoc := CoDOMDocument.Create;
XMLDoc.load('xml地址');
ClientCardList:=XMLDoc.selectNodes('/dataSources/dataSource);
for I :=0 to ClientCardList.length-1 do
begin
ClientCardNode.getAttribute('name');//取得name值
...
end;
end;
var
XMLDoc :IXMLDOMDocument;
ClientCardList:IXMLDOMNodeList;
ClientCardNode:IXMLDOMElement;
I:integer;
strName:string;
begin
XMLDoc := CoDOMDocument.Create;
XMLDoc.load('D:\数据\data.xml');
ClientCardList:=XMLDoc.selectNodes('/dataSources/dataSource');
for I :=0 to ClientCardList.length-1 do
begin
strName:= strName+ ClientCardNode.getAttribute('name');//取得name值
end;
end;注:程序运行到 ClientCardNode.getAttribute('name');这里就报错,是怎么回事呀,谢谢
IXMLDataSourceType = interface;{ IXMLDataSourcesType } IXMLDataSourcesType = interface(IXMLNodeCollection)
['{CB396D6E-8946-4E69-B3D4-07EC4A21E568}']
{ Property Accessors }
function GetDataSource(Index: Integer): IXMLDataSourceType;
{ Methods & Properties }
function Add: IXMLDataSourceType;
function Insert(const Index: Integer): IXMLDataSourceType;
property DataSource[Index: Integer]: IXMLDataSourceType read GetDataSource; default;
end;{ IXMLDataSourceType } IXMLDataSourceType = interface(IXMLNode)
['{A3FA4B8D-7BED-4611-B024-3CB01D23C868}']
{ Property Accessors }
function GetName: string;
function GetValue: string;
function GetFlag: Boolean;
procedure SetName(Value: string);
procedure SetValue(Value: string);
procedure SetFlag(Value: Boolean);
{ Methods & Properties }
property Name: string read GetName write SetName;
property Value: string read GetValue write SetValue;
property Flag: Boolean read GetFlag write SetFlag;
end;{ Forward Decls } TXMLDataSourcesType = class;
TXMLDataSourceType = class;{ TXMLDataSourcesType } TXMLDataSourcesType = class(TXMLNodeCollection, IXMLDataSourcesType)
protected
{ IXMLDataSourcesType }
function GetDataSource(Index: Integer): IXMLDataSourceType;
function Add: IXMLDataSourceType;
function Insert(const Index: Integer): IXMLDataSourceType;
public
procedure AfterConstruction; override;
end;{ TXMLDataSourceType } TXMLDataSourceType = class(TXMLNode, IXMLDataSourceType)
protected
{ IXMLDataSourceType }
function GetName: string;
function GetValue: string;
function GetFlag: Boolean;
procedure SetName(Value: string);
procedure SetValue(Value: string);
procedure SetFlag(Value: Boolean);
end;{ Global Functions }function GetdataSources(Doc: IXMLDocument): IXMLDataSourcesType;
function LoaddataSources(const FileName: WideString): IXMLDataSourcesType;
function NewdataSources: IXMLDataSourcesType;const
TargetNamespace = '';implementation{ Global Functions }function GetdataSources(Doc: IXMLDocument): IXMLDataSourcesType;
begin
Result := Doc.GetDocBinding('dataSources', TXMLDataSourcesType, TargetNamespace) as IXMLDataSourcesType;
end;function LoaddataSources(const FileName: WideString): IXMLDataSourcesType;
begin
Result := LoadXMLDocument(FileName).GetDocBinding('dataSources', TXMLDataSourcesType, TargetNamespace) as IXMLDataSourcesType;
end;function NewdataSources: IXMLDataSourcesType;
begin
Result := NewXMLDocument.GetDocBinding('dataSources', TXMLDataSourcesType, TargetNamespace) as IXMLDataSourcesType;
end;{ TXMLDataSourcesType }procedure TXMLDataSourcesType.AfterConstruction;
begin
RegisterChildNode('dataSource', TXMLDataSourceType);
ItemTag := 'dataSource';
ItemInterface := IXMLDataSourceType;
inherited;
end;function TXMLDataSourcesType.GetDataSource(Index: Integer): IXMLDataSourceType;
begin
Result := List[Index] as IXMLDataSourceType;
end;function TXMLDataSourcesType.Add: IXMLDataSourceType;
begin
Result := AddItem(-1) as IXMLDataSourceType;
end;function TXMLDataSourcesType.Insert(const Index: Integer): IXMLDataSourceType;
begin
Result := AddItem(Index) as IXMLDataSourceType;
end;{ TXMLDataSourceType }function TXMLDataSourceType.GetName: string;
begin
Result := AttributeNodes['name'].Text;
end;procedure TXMLDataSourceType.SetName(Value: string);
begin
SetAttribute('name', Value);
end;function TXMLDataSourceType.GetValue: string;
begin
Result := AttributeNodes['value'].Text;
end;procedure TXMLDataSourceType.SetValue(Value: string);
begin
SetAttribute('value', Value);
end;function TXMLDataSourceType.GetFlag: Boolean;
begin
Result := AttributeNodes['flag'].NodeValue;
end;procedure TXMLDataSourceType.SetFlag(Value: Boolean);
begin
SetAttribute('flag', Value);
end;end.
即可
File-> new -> other... -> xml data binding
试试看……
var
XMLDoc :IXMLDOMDocument;
ClientCardList:IXMLDOMNodeList;
i:integer;
strName:string;
begin
XMLDoc := CoDOMDocument.Create;
XMLDoc.load('D:\数据\data.xml');
ClientCardList:=XMLDoc.selectNodes('/dataSources/dataSource');
for i :=0 to ClientCardList.length-1 do
begin
strName:=strName+ ClientCardList[i].attributes.getnameditem('name').nodeValue;
end;
end;
我晕,终于搞定了:
var
XMLDoc :IXMLDOMDocument;
ClientCardList:IXMLDOMNodeList;
i:integer;
strName:string;
begin
XMLDoc := CoDOMDocument.Create;
XMLDoc.load('D:\数据\data.xml');
ClientCardList:=XMLDoc.selectNodes('/dataSources/dataSource');
for i :=0 to ClientCardList.length-1 do
begin
strName:=strName+ ClientCardList[i].attributes.getnameditem('name').nodeValue;
end;
end;利用上述算法的话,楼主你的添加,删除,修改方法每个都要去写,而且扩展和维护也不会很方便