procedure TMyTemplate.SaveToXmlFile(qry: TQuery);
var
str: widestring;
XML: IXMLDocument;
pNode: IXMLNode;
nodeList, attrList: IXMLNodeList;
i: Integer;
begin
if not Assigned(qry) or not qry.Active or (qry.RecordCount < 3) then begin
Say('定位信息不存在或不足(最少应有3条记录).');
exit;
end;
XML := TXMLDocument.Create(FXMLFile);
XML.Active := True;
pNode := xml.DocumentElement;
if not pNode.HasChildNodes then Exit;
attrList := pNode.AttributeNodes; {根节点的属性列表}
// FCompany := attrList['company'].NodeValue;
// FModelName := attrList['modelname'].NodeValue;
nodeList := pNode.ChildNodes; {根节点下的子节点列表}
qry.First;
// AttributeNodes['count'].NodeValue := 2; 第一条记录 是图像有效范围矩形
attrList['x1'].NodeValue := qry.FieldByName('x1').AsInteger;
attrList['y1'].NodeValue := qry.FieldByName('y1').AsInteger;
attrList['x2'].NodeValue := qry.FieldByName('x2').AsInteger;
attrList['y2'].NodeValue := qry.FieldByName('y2').AsInteger;
try
with nodeList['IdentifyAreas'].ChildNodes['Area1'] do begin
qry.Next; // 规定 第 2, 3条记录 是 第一识别区
// AttributeNodes['eName'].NodeValue := qry.FieldByName('FieldName').AsString;
// AttributeNodes['viewx'].NodeValue := qry.FieldByName('viewx').AsInteger;
// AttributeNodes['viewy'].NodeValue := qry.FieldByName('viewy').AsInteger;
AttributeNodes['x1'].NodeValue := qry.FieldByName('x1').AsInteger;
AttributeNodes['y1'].NodeValue := qry.FieldByName('y1').AsInteger;
AttributeNodes['x2'].NodeValue := qry.FieldByName('x2').AsInteger;
AttributeNodes['y2'].NodeValue := qry.FieldByName('y2').AsInteger;
end;
with nodeList['IdentifyAreas'].ChildNodes['Area2'] do begin
qry.Next;
AttributeNodes['x1'].NodeValue := qry.FieldByName('x1').AsInteger;
AttributeNodes['y1'].NodeValue := qry.FieldByName('y1').AsInteger;
AttributeNodes['x2'].NodeValue := qry.FieldByName('x2').AsInteger;
AttributeNodes['y2'].NodeValue := qry.FieldByName('y2').AsInteger;
end;
// 调整 分块节点个数
FIdentifyAreasCount := 2;
FBlockCount := qry.RecordCount - 3;
nodeList['blocks'].AttributeNodes['count'].NodeValue := FBlockCount;
// nodeList['blocks'].ChildNodes.Clear;
i := 0;
while nodeList['blocks'].ChildNodes.Count < FBlockCount do begin
i := i + 1;
str := 'b' + IntToStr(i);
pNode := nodeList['blocks'];
pNode.AddChild(str);
// nodeList['blocks'].AddChild(str, -1); 编译通不过 用上面两行替代
end;
while nodeList['blocks'].ChildNodes.Count > FBlockCount do begin
i := nodeList['blocks'].ChildNodes.Count - 1;
pNode := nodeList['blocks'];
pNode.ChildNodes.Delete(i);
// nodeList['blocks'].ChildNodes.Delete(i); 编译通不过 用上面两行替代
end;
// blocks 节点
qry.Next; // 指向第一个区块
for i := 0 to FBlockCount - 1 do begin
with nodeList['blocks'].ChildNodes[i] do begin
AttributeNodes['x1'].NodeValue := qry.FieldByName('x1').AsInteger;
AttributeNodes['y1'].NodeValue := qry.FieldByName('y1').AsInteger;
AttributeNodes['x2'].NodeValue := qry.FieldByName('x2').AsInteger;
AttributeNodes['y2'].NodeValue := qry.FieldByName('y2').AsInteger;
end;
qry.Next;
end;
XML.SaveToFile(FXMLFile);
Finally
XML.Active := false;
end;
end;
转载于:https://my.oschina.net/u/582827/blog/223458