xml文件通用打开方式

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文件通用打开方式

上一篇:nacos中拓展组动态配置


下一篇:Dos命令