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;