//数据集生成Xml 格式文件
function makeXml(Dataset:TADODataSet;XMLRootName:string):Integer;
var
i : Integer;
xml : String;
temp:Variant;
doc : IXMLDOMDocument;
root,child,child1 : IXMLDomElement;
a:TFieldType;
{函数功能:将Dataset 转换成 XML 格式文件}
begin
try
if Dataset.Active =false then Dataset.Active:=true;
xml:=XMLRootName;
//doc --> root --> first level(child) --> second level(child1) --> child1 value
doc := CoDOMDocument60.Create; // 调用 MS XML 6.0 控件建立 DOC --> IXMLDomDocument3
//设置 xml 根节点
root := doc.createElement(xml); //建立 IXMLDomDocument 的根节点元素 root --> IXMLDOMElement
doc.appendchild(root);
// 生成 xml 文件过程
//while not table.eof do
with DataSet do
begin
while not eof do
begin
//添加第一层 节点 , Records
child:= doc.createElement('Records'); //建立 root 根节点的第一层节点元素 child --> IXMLDOMElement
root.appendchild(child);
for i:=0 to FieldCount-1 do
begin
//添加第二层子节点 字段名与值
child1:=doc.createElement(Fields[i].FieldName); //建立 child 第一层节点元素的第二层节点元素 child1 --> IXMLDOMElement
child.appendchild(child1); //字段名
//检查字段 类型 ,赋值给 temp
case TFieldType(Ord(Fields[i].DataType)) of
ftString,ftWideString:
begin
if Fields[i].AsString ='' then
temp :=' ' //Put a default string
else
temp := Fields[i].AsString;
end;
ftInteger,ftLargeint, ftWord, ftSmallint:
begin
if Fields[i].AsInteger > 0 then
temp := IntToStr(Fields[i].AsInteger)
else
temp := '0';
end;
ftFloat, ftCurrency, ftBCD:
begin
if Fields[i].AsFloat > 0 then
temp := FloatToStr(Fields[i].AsFloat)
else
temp := '0';
end;
ftBoolean:
begin
if Fields[i].Value then
temp:= True
else
temp:= False;
end;
ftDate:
begin
if (not Fields[i].IsNull) or
(Length(Trim(Fields[i].AsString)) > 0) then
temp := FormatDateTime('MM/DD/YYYY',Fields[i].AsDateTime)
else
temp:= '01/01/2000'; //预设日期
end;
ftDateTime:
begin
if (not Fields[i].IsNull) or
(Length(Trim(Fields[i].AsString)) > 0) then
temp := FormatDateTime('MM/DD/YYYY hh:nn:ss',Fields[i].AsDateTime)
else
temp := '01/01/2000 00:00:00'; //预设日期时间
end;
ftTime:
begin
if (not Fields[i].IsNull) or
(Length(Trim(Fields[i].AsString)) > 0) then
temp := FormatDateTime('hh:nn:ss',Fields[i].AsDateTime)
else
temp := '00:00:00'; //预设时间
end;
end;
//
child1.appendChild(doc.createTextNode(temp)); // 字段名对应的值
end;
Next;
end; // eof
end; // with
XMLStr:=doc.xml;
//memo1.lines.Append(doc.xml);
Result:=1;
except
on e:Exception do
Result:=-1;
end;
end;
function makeXml(Dataset:TADODataSet;XMLRootName:string):Integer;
var
i : Integer;
xml : String;
temp:Variant;
doc : IXMLDOMDocument;
root,child,child1 : IXMLDomElement;
a:TFieldType;
{函数功能:将Dataset 转换成 XML 格式文件}
begin
try
if Dataset.Active =false then Dataset.Active:=true;
xml:=XMLRootName;
//doc --> root --> first level(child) --> second level(child1) --> child1 value
doc := CoDOMDocument60.Create; // 调用 MS XML 6.0 控件建立 DOC --> IXMLDomDocument3
//设置 xml 根节点
root := doc.createElement(xml); //建立 IXMLDomDocument 的根节点元素 root --> IXMLDOMElement
doc.appendchild(root);
// 生成 xml 文件过程
//while not table.eof do
with DataSet do
begin
while not eof do
begin
//添加第一层 节点 , Records
child:= doc.createElement('Records'); //建立 root 根节点的第一层节点元素 child --> IXMLDOMElement
root.appendchild(child);
for i:=0 to FieldCount-1 do
begin
//添加第二层子节点 字段名与值
child1:=doc.createElement(Fields[i].FieldName); //建立 child 第一层节点元素的第二层节点元素 child1 --> IXMLDOMElement
child.appendchild(child1); //字段名
//检查字段 类型 ,赋值给 temp
case TFieldType(Ord(Fields[i].DataType)) of
ftString,ftWideString:
begin
if Fields[i].AsString ='' then
temp :=' ' //Put a default string
else
temp := Fields[i].AsString;
end;
ftInteger,ftLargeint, ftWord, ftSmallint:
begin
if Fields[i].AsInteger > 0 then
temp := IntToStr(Fields[i].AsInteger)
else
temp := '0';
end;
ftFloat, ftCurrency, ftBCD:
begin
if Fields[i].AsFloat > 0 then
temp := FloatToStr(Fields[i].AsFloat)
else
temp := '0';
end;
ftBoolean:
begin
if Fields[i].Value then
temp:= True
else
temp:= False;
end;
ftDate:
begin
if (not Fields[i].IsNull) or
(Length(Trim(Fields[i].AsString)) > 0) then
temp := FormatDateTime('MM/DD/YYYY',Fields[i].AsDateTime)
else
temp:= '01/01/2000'; //预设日期
end;
ftDateTime:
begin
if (not Fields[i].IsNull) or
(Length(Trim(Fields[i].AsString)) > 0) then
temp := FormatDateTime('MM/DD/YYYY hh:nn:ss',Fields[i].AsDateTime)
else
temp := '01/01/2000 00:00:00'; //预设日期时间
end;
ftTime:
begin
if (not Fields[i].IsNull) or
(Length(Trim(Fields[i].AsString)) > 0) then
temp := FormatDateTime('hh:nn:ss',Fields[i].AsDateTime)
else
temp := '00:00:00'; //预设时间
end;
end;
//
child1.appendChild(doc.createTextNode(temp)); // 字段名对应的值
end;
Next;
end; // eof
end; // with
XMLStr:=doc.xml;
//memo1.lines.Append(doc.xml);
Result:=1;
except
on e:Exception do
Result:=-1;
end;
end;