本人写了一个生成XML文件的函数。可是生成XML文件没有
<?xml version="1.0" encoding="GBK"?>
这一行,为什么?怎么生成?
生成函数如下:
function TfrmDataTrans.makeXml(qrytmp:TADOQuery;sTableName :String):Integer;
var
i,Count :Integer;
xml,temp :String;
Doc : IXMLDOMDocument;
Root,Child,ChildNode,Head : IXMLDomElement;
begin
try
with qrytmp do
begin
Close;
SQL.Clear;
SQL.Text := 'select * from '+sTableName;
Open;
if Recordcount = 0 then
begin
mmoText.Lines.Add('表【'+sTableName+'】无上传记录');
Exit;
end;
end;
xml := sTableName;
Doc := CreateDOMDocument;
Root := doc.createElement(xml); //把表名写入XML根节
Doc.appendchild(root);;
Count := 1 ;
gProcess.MinValue := 0 ;
gProcess.MaxValue := qrytmp.RecordCount - 1 ;
while not qrytmp.Eof do
begin
gProcess.Progress := Count ;
child:= doc.createElement('Records'); //添加记录节点,为了分开每条记录
root.appendchild(child);
for i:=0 to qrytmp.FieldCount - 1 do
begin
ChildNode:=Doc.createElement(qrytmp.Fields[i].FieldName);
Child.appendchild(ChildNode);
case TFieldType(Ord(qrytmp.Fields[i].DataType)) of
ftString:
begin
if qrytmp.Fields[i].AsString ='' then
temp :='null'
else
temp := qrytmp.Fields[i].AsString;
end;
ftInteger, ftWord, ftSmallint:
begin
if qrytmp.Fields[i].AsInteger > 0 then
temp := IntToStr(qrytmp.Fields[i].AsInteger)
else
temp := '0';
end;
ftFloat, ftCurrency, ftBCD:
begin
if qrytmp.Fields[i].AsFloat > 0 then
temp := FloatToStr(qrytmp.Fields[i].AsFloat)
else
temp := '0';
end;
ftBoolean:
begin
if qrytmp.Fields[i].Value then
temp:= 'True'
else
temp:= 'False';
end;
ftDate:
begin
if (not qrytmp.Fields[i].IsNull) or (Length(Trim(qrytmp.Fields[i].AsString)) > 0) then
temp := FormatDateTime('yyyy-mm-dd',qrytmp.Fields[i].AsDateTime)
else
temp:= '2000-01-01';
end;
ftDateTime:
begin
if (not qrytmp.Fields[i].IsNull) or (Length(Trim(qrytmp.Fields[i].AsString)) > 0) then
temp := FormatDateTime('yyyy-mm-dd hh:nn:ss', qrytmp.Fields[i].AsDateTime)
else
temp := '2000-01-01 00:00:00';
end;
ftTime:
begin
if (not qrytmp.Fields[i].IsNull) or (Length(Trim(qrytmp.Fields[i].AsString)) > 0) then
temp := FormatDateTime('hh:nn:ss',qrytmp.Fields[i].AsDateTime)
else
temp := '00:00:00';
end;
end;
ChildNode.appendChild(doc.createTextNode(temp)); end;
Count := Count + 1 ;
qrytmp.Next;
end;
Doc.save(dirlstTrans.Directory+'\'+xml+'.xml');
Doc := nil;
Result:=1;
except
on e:Exception do
Result:=-1;
end;
mmoText.Lines.Add('共'+inttostr(qrytmp.RecordCount)+'记录,对'+inttostr(Count-1)+'条记录打包');
qrytmp.Close;
end;
生成xml文件如下,而且有时候中文显示为乱码
- <T_Res_Application>
- <Records>
<uuid>40288026193734700119373667350003</uuid>
<name>海事收费系统客户端</name>
<serial_index>3</serial_index>
<enabled>1</enabled>
<home_page>null</home_page>
<help_url>null</help_url>
<description>null</description>
<developer>null</developer>
<version>1.0</version>
<create_time>2008-04-28 15:00:00</create_time>
</Records>
</T_Res_Application>
请高手们看看,满意肯定结贴给分
<?xml version="1.0" encoding="GBK"?>
这一行,为什么?怎么生成?
生成函数如下:
function TfrmDataTrans.makeXml(qrytmp:TADOQuery;sTableName :String):Integer;
var
i,Count :Integer;
xml,temp :String;
Doc : IXMLDOMDocument;
Root,Child,ChildNode,Head : IXMLDomElement;
begin
try
with qrytmp do
begin
Close;
SQL.Clear;
SQL.Text := 'select * from '+sTableName;
Open;
if Recordcount = 0 then
begin
mmoText.Lines.Add('表【'+sTableName+'】无上传记录');
Exit;
end;
end;
xml := sTableName;
Doc := CreateDOMDocument;
Root := doc.createElement(xml); //把表名写入XML根节
Doc.appendchild(root);;
Count := 1 ;
gProcess.MinValue := 0 ;
gProcess.MaxValue := qrytmp.RecordCount - 1 ;
while not qrytmp.Eof do
begin
gProcess.Progress := Count ;
child:= doc.createElement('Records'); //添加记录节点,为了分开每条记录
root.appendchild(child);
for i:=0 to qrytmp.FieldCount - 1 do
begin
ChildNode:=Doc.createElement(qrytmp.Fields[i].FieldName);
Child.appendchild(ChildNode);
case TFieldType(Ord(qrytmp.Fields[i].DataType)) of
ftString:
begin
if qrytmp.Fields[i].AsString ='' then
temp :='null'
else
temp := qrytmp.Fields[i].AsString;
end;
ftInteger, ftWord, ftSmallint:
begin
if qrytmp.Fields[i].AsInteger > 0 then
temp := IntToStr(qrytmp.Fields[i].AsInteger)
else
temp := '0';
end;
ftFloat, ftCurrency, ftBCD:
begin
if qrytmp.Fields[i].AsFloat > 0 then
temp := FloatToStr(qrytmp.Fields[i].AsFloat)
else
temp := '0';
end;
ftBoolean:
begin
if qrytmp.Fields[i].Value then
temp:= 'True'
else
temp:= 'False';
end;
ftDate:
begin
if (not qrytmp.Fields[i].IsNull) or (Length(Trim(qrytmp.Fields[i].AsString)) > 0) then
temp := FormatDateTime('yyyy-mm-dd',qrytmp.Fields[i].AsDateTime)
else
temp:= '2000-01-01';
end;
ftDateTime:
begin
if (not qrytmp.Fields[i].IsNull) or (Length(Trim(qrytmp.Fields[i].AsString)) > 0) then
temp := FormatDateTime('yyyy-mm-dd hh:nn:ss', qrytmp.Fields[i].AsDateTime)
else
temp := '2000-01-01 00:00:00';
end;
ftTime:
begin
if (not qrytmp.Fields[i].IsNull) or (Length(Trim(qrytmp.Fields[i].AsString)) > 0) then
temp := FormatDateTime('hh:nn:ss',qrytmp.Fields[i].AsDateTime)
else
temp := '00:00:00';
end;
end;
ChildNode.appendChild(doc.createTextNode(temp)); end;
Count := Count + 1 ;
qrytmp.Next;
end;
Doc.save(dirlstTrans.Directory+'\'+xml+'.xml');
Doc := nil;
Result:=1;
except
on e:Exception do
Result:=-1;
end;
mmoText.Lines.Add('共'+inttostr(qrytmp.RecordCount)+'记录,对'+inttostr(Count-1)+'条记录打包');
qrytmp.Close;
end;
生成xml文件如下,而且有时候中文显示为乱码
- <T_Res_Application>
- <Records>
<uuid>40288026193734700119373667350003</uuid>
<name>海事收费系统客户端</name>
<serial_index>3</serial_index>
<enabled>1</enabled>
<home_page>null</home_page>
<help_url>null</help_url>
<description>null</description>
<developer>null</developer>
<version>1.0</version>
<create_time>2008-04-28 15:00:00</create_time>
</Records>
</T_Res_Application>
请高手们看看,满意肯定结贴给分
解决方案 »
- 关于FastReport的条码问题
- 我自己开发了一个网页浏览器,设置默认浏览器为什么不成功呢???
- Flash.OCX问题,请教高手
- 哪有photoshop中的魔棒算法?
- aiirii(ari-爱的眼睛),把如何在DELPHI中对USB口进行操作发给我?
- 操作foxpro数据库必须配置数据源吗?
- 用TDXSound組件的WaitForMultipleObjects結果總是WAIT_OBJECT_0
- delphi中表示一秒(00:00:01)的数是什么?
- 如何实现繁简体字的互换(我有繁简体字对照表)
- ‘1’的虚拟键是什么?
- 关于DLL中TComPort控件的事件响应问题
- 100分.不同的List,存储了不同的对象,请问如何用同一函数去检查List是否重复
是XML串的前缀,version表示版本号,encoding表示XML编码格式下面的XML串有乱码,就是因为没设置encoding="GBK",
Doc.Encoding := 'UTF-8';//加上编码规则
或
Doc.Encoding := 'GB2312';//试试
碰到类似问题,找不到 IXMLDOMDocument 接口如何指定版本号以及编码格式,可采用 TXMLDocument组件。procedure SaveSetting(sName: PChar); stdcall;
var
i,j: integer;
xmlDoc: TXmlDocument;
root,block,element: IXMLNode;
begin
xmlDoc := TXmlDocument.Create(nil);
xmlDoc.Active := true;
xmlDoc.Version := '1.0';
xmlDoc.Encoding := 'GB2312';//或者GBK
root := xmlDoc.AddChild('config');
for i:=0 to tmp_Blocks.nBlock-1 do
begin
block := root.AddChild('block');
block.Attributes['type'] := tmp_Blocks.blocks[i].nType;
block.Attributes['name'] := tmp_Blocks.blocks[i].blkName;
block.Attributes['re']:= tmp_Blocks.blocks[i].re;
block.Attributes['state'] := tmp_Blocks.blocks[i].state;
for j:=0 to tmp_Blocks.blocks[i].nElement-1 do
begin
element := block.AddChild('element');
element.Attributes['type'] := tmp_Blocks.blocks[i].elements[j].nType;
element.Attributes['name'] := tmp_Blocks.blocks[i].elements[j].name;
end;
end;
CopySetting(tmp_Blocks,g_Blocks);
XmlDoc.SaveToFile(sName);
XmlDoc.Active := false;
XmlDoc.Free;
end;
xmlDoc: TXmlDocument;//假设你的对象是xmlDoc加上这下面这个
xmlDoc.version:='1.0'
xmlDoc.encoding:='GB2312';加在创建对象的后面即可