码迷,mamicode.com
首页 > 其他好文 > 详细

xml文件通用打开方式

时间:2021-06-28 18:17:02      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:bind   array   read   val   init   active   taf   document   edit   

unit DataStructUnit;

interface

type
txmlvulrd =record
sname:string;
svul:string;
end;

txmlbasedDataStruct = class
private
Fifissubitem: Boolean;

function rattrslen: Integer;
function rstructslen: Integer;
procedure wattrslen(const Value: Integer);
procedure wstructslen(const Value: Integer);
procedure Setifissubitem(const Value: Boolean);



public
  name:string;
  vul:string;

  attrdatas : TArray<txmlvulrd>;

// structdatas : TArray;

  property ifissubitem : Boolean read Fifissubitem write Setifissubitem;

  property attrslen : Integer read rattrslen write wattrslen;
  property structslen : Integer read rstructslen write wstructslen;
  procedure Addattritemdata(rind:Integer;rsname,rsvul:string);
  procedure Addstructitemdata(rind:Integer;rsname,rsvul:string);
  constructor create;

  procedure Cleardatas;

end;

txmlDataStruct = class(txmlbasedDataStruct)
private

public
  filepath:string;
  subdatastructs: TArray<txmlDataStruct>;

  procedure addsubitem(rbaseitem:txmlDataStruct);

  function Getxmlcntfromname(rname:string):Integer;

  function Getxmldatafromname(rname:string; rcn:Integer=0): txmlDataStruct;

  function getxmldatatoname(rname:string): txmlDataStruct;

  constructor create;

  procedure Clearxmldatas;

end;
//******************************************************************

tsubdatatyp = record
  subind:Integer;
  subname:string;
  typename:string;
  subbitsize:Integer;
  subbitoffs:Integer;

end;

tdatatyp = record
  typename:string;
  bitsize:Integer;
end;

implementation

{ txmlDataStruct }

procedure txmlDataStruct.addsubitem(rbaseitem: txmlDataStruct);
begin
SetLength(subdatastructs,Length(subdatastructs)+1);
subdatastructs[Length(subdatastructs)-1]:= rbaseitem;
end;

procedure txmlDataStruct.Clearxmldatas;
begin
SetLength(subdatastructs,0);
Cleardatas;
end;

constructor txmlDataStruct.create;
begin
filepath:=‘‘;
end;
//device下 查找数据
function txmlDataStruct.Getxmlcntfromname(rname: string): Integer;
var
i,cc,cn:Integer;
begin
cc:=0;
cn:=Length(subdatastructs);
for i := 0 to cn-1 do
begin
if subdatastructs[i].name=rname then
inc(cc);
end;
Result:=cc;
end;

function txmlDataStruct.Getxmldatafromname(rname: string; rcn:Integer=0): txmlDataStruct;
var
i,cc,cn:Integer;
begin
cc:=0;
cn:=Length(subdatastructs);
Result:=nil;
for I := 0 to cn-1 do
begin
if subdatastructs[i].name=rname then
inc(cc);
if cc=rcn+1 then
begin
Result:=subdatastructs[i];
Exit;
end;
end;
end;

function txmlDataStruct.getxmldatatoname(rname: string): txmlDataStruct;

var
rb:Boolean;
i,cn:Integer;
// tempdata:txmlDataStruct;
begin
Result:=nil;
if name<>rname then
begin
cn:=Length(subdatastructs);
for i := 0 to cn-1 do
begin
Result:= subdatastructs[i].getxmldatatoname(rname);
end;
end
else
Result:=Self;
end;

{ txmlbasedDataStruct }

procedure txmlbasedDataStruct.Addattritemdata(rind:Integer; rsname, rsvul: string);
begin
attrdatas[rind].sname:=rsname;
attrdatas[rind].svul:=rsvul;
end;

procedure txmlbasedDataStruct.Addstructitemdata(rind:Integer; rsname, rsvul: string);
begin

end;

procedure txmlbasedDataStruct.Cleardatas;
begin
ifissubitem:=False;
SetLength(attrdatas,0);
end;

constructor txmlbasedDataStruct.create;
begin
attrslen:=0;
structslen:=0;
ifissubitem:=False;
end;

function txmlbasedDataStruct.rattrslen: Integer;
begin
Result:=Length(attrdatas) ;
end;

function txmlbasedDataStruct.rstructslen: Integer;
begin

end;

procedure txmlbasedDataStruct.Setifissubitem(const Value: Boolean);
begin
Fifissubitem := Value;
end;

procedure txmlbasedDataStruct.wattrslen(const Value: Integer);
begin
SetLength(attrdatas,Value) ;
end;

procedure txmlbasedDataStruct.wstructslen(const Value: Integer);
begin

end;

end.

//引用该单元即可
获取方式:
procedure ttheviewcontroller.GetXMLfileInfo;
var
Xmld: IXMLDocument;
thenode, anode: IXMLNode;
i,c,d:Integer;
rstr,rsname:string;
thexmlitem:txmlDataStruct;
begin
CoInitialize(nil);
Xmld := NewXMLDocument();
try
Xmld.LoadFromFile(xmlfilepath);
// uishowdata(Xmld.FileName);
Xmld.Active := True;

thexmlitem.filepath := Xmld.FileName;

thenode := Xmld.DocumentElement;
c:=thenode.ChildNodes.Count;

rstr:=thenode.LocalName;

// uishowdata(rstr);
thexmlitem.name:=rstr;
if c>0 then
thexmlitem.ifissubitem:=True
else
thexmlitem.ifissubitem:=False;

d:=thenode.AttributeNodes.Count;
thexmlitem.attrslen:=d;
for i := 0 to d-1 do
begin
  rstr:=thenode.AttributeNodes.Get(i).text;
  rsname:= thenode.AttributeNodes.Get(i).LocalName;
  thexmlitem.Addattritemdata(i,rsname,rstr);

// uishowdata(rstr+rsname);
end;

for i := 0 to c-1 do
begin

// xmlbaseditem:=txmlbasedDataStruct.create;
anode:= thenode.ChildNodes.Get(i);
thexmlitem.addsubitem(releasetheXMLNode(anode));

// mmo1.Lines.Add(‘NO.‘+IntToStr(dn));
end;
finally
Xmld := nil;
CoUnInitialize;
end;
end;

xml文件通用打开方式

标签:bind   array   read   val   init   active   taf   document   edit   

原文地址:https://www.cnblogs.com/yxyang/p/14933340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!