xml 写入数据

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值