xml 写入数据

前端之家收集整理的这篇文章主要介绍了xml 写入数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
procedureTMyTemplate.SaveToXmlFile(qry:TQuery);
var
str:widestring;
XML:IXMLDocument;
pNode:IXMLNode;
nodeList,attrList:IXMLNodeList;
i:Integer;
begin
ifnotAssigned(qry)ornotqry.Activeor(qry.RecordCount<3)thenbegin
Say('定位信息不存在或不足(最少应有3条记录).');
exit;
end;
XML:=TXMLDocument.Create(FXMLFile);
XML.Active:=True;
pNode:=xml.DocumentElement;
ifnotpNode.HasChildNodesthenExit;
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
withnodeList['IdentifyAreas'].ChildNodes['Area1']dobegin
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;
withnodeList['IdentifyAreas'].ChildNodes['Area2']dobegin
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;
whilenodeList['blocks'].ChildNodes.Count<FBlockCountdobegin
i:=i+1;
str:='b'+IntToStr(i);
pNode:=nodeList['blocks'];
pNode.AddChild(str);
//nodeList['blocks'].AddChild(str,-1); 编译通不过用上面两行替代
end;
whilenodeList['blocks'].ChildNodes.Count>FBlockCountdobegin
i:=nodeList['blocks'].ChildNodes.Count-1;
pNode:=nodeList['blocks'];
pNode.ChildNodes.Delete(i);
//nodeList['blocks'].ChildNodes.Delete(i); 编译通不过用上面两行替代
end;
//blocks节点
qry.Next;//指向第一个区块
fori:=0toFBlockCount-1dobegin
withnodeList['blocks'].ChildNodes[i]dobegin
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;

猜你在找的XML相关文章