1,txt文件解析
//新使用的txt解析函数
procedure TFrmImportAfterHourFundMarket.TxtPrepareData;
var
F1:TextFile;
sLine:string;//txt文件得到每一行数据
AStr: string;//每一行需要处理的数据
idx: Integer;//得到分隔符的下标
ASubStr: string;//每一行分割的数据
sTraller:string;//结束符
i:Integer;
S1:string;
S2:string;
S3:string;
begin
MemoryTable.FieldDefs.Add('S1',ftString,6);
MemoryTable.FieldDefs.Add('S2',ftString,8);
MemoryTable.FieldDefs.Add('S3',ftFloat,0);
if not MemoryTable.Active then MemoryTable.Active:=True;
// 解析txt文件
AssignFile(F1,fsDBFPath+'mktdt00.txt');
Reset(F1);
ReadOneLine(F1,FILEFORMAT_U,sLine);//读第一行数据
AStr := trim(sLine);
i := 0;
while Pos('|', AStr) > 0 do
begin
idx := Pos('|', AStr);
ASubStr := Copy(AStr, 1, idx - 1);
AStr := Copy(AStr, idx + 1, Length(AStr));
inc(i);
if i = 7 then//这里取suo
S6 := Copy(ASubStr,1,8);
end;
while not eof(F1) do
begin
ReadOneLine(F1,FILEFORMAT_U,sLine); //从第二行开始读取数据
AStr := trim(sLine);
i := 0;
while Pos('|', AStr) > 0 do
begin
idx := Pos('|', AStr);
ASubStr := Copy(AStr, 1, idx - 1);
AStr := Copy(AStr, idx + 1, Length(AStr));
inc(i);
if i = 1 then
sTraller := ASubStr
else if i = 2 then
S1 := ASubStr
else if i = 3 then
S2 := ASubStr
else if i = 6 then
S3 := ASubStr;
end;
if sTraller <> 'TRAILER' then//如果不是最后一行数据则插入内存表
begin
MemoryTable.Append;
MemoryTable.FieldByName('S1').AsString:=S1;
MemoryTable.FieldByName('S2').AsString:=S2;
MemoryTable.FieldByName('S3').AsString:=S3;
MemoryTable.Post;
end;
end;
CloseFile(F1);
end;
|
2,xml文件解析,这里需要用到Capital目录下的
MSXML_TLB.pas,注意dpr中需要添加引用
//新使用的xml解析函数
procedure TFrmImportAfterHourFundMarket.XmlPrepareData;
var
doc :IXMLDOMDocument;
nList :IXMLDOMNodeList;
nList1 :IXMLDOMNodeList;//指数代码
nList2 :IXMLDOMNodeList;//指数名称
nList3 :IXMLDOMNodeList;//收盘价
RecorderCount:Integer;
i:Integer;
HQZQDM:string;
HQZQJC:string;
HQZRSP:string;
begin
MemoryTable.FieldDefs.Add('HQZQDM',ftString,6);
MemoryTable.FieldDefs.Add('HQZQJC',ftString,8);
MemoryTable.FieldDefs.Add('HQZRSP',ftFloat,0);
if not MemoryTable.Active then MemoryTable.Active:=True;
//解析xml文件
doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument;
doc.load(fsDBFPath+'securities_'+fsLastSysdate+'.xml');
//获取xml节点列表
nList := doc.getElementsByTagName('Security');
nList1 := doc.getElementsByTagName('SecurityID');
nList2 := doc.getElementsByTagName('Symbol');
nList3 := doc.getElementsByTagName('PrevClosePx');
//获取指数代码节点数据
RecorderCount :=nList.Get_length;
for i:=0 to RecorderCount - 1 do
begin
HQZQDM := nList1.item[i].Get_text;//获取节点的值
HQZQJC := nList2.item[i].Get_text;
HQZRSP := nList3.item[i].Get_text;
MemoryTable.Append;
MemoryTable.FieldByName('HQZQDM').AsString:=HQZQDM;
MemoryTable.FieldByName('HQZQJC').AsString:=HQZQJC;
MemoryTable.FieldByName('HQZRSP').AsString:=HQZRSP;
MemoryTable.Post;
end;
end;
|