我用DOM生成XML文件,XML文件内有些值为中文字符,生成后显示为乱码,我试过gb2312/uft8/gbk都同样结果
代码如下:
procedure Tfrm_xml.Button2Click(Sender: TObject);
var
xdoc: TXMLDocument; // variable to document
noraiz, nopai, nofilho: TDOMNode; // variable to nodes
begin
//create a document xdoc := TXMLDocument.create;
xdoc.Encoding := 'gb2312'; //create a root node
noraiz := xdoc.CreateElement('cadastro');
Xdoc.Appendchild(noraiz); // save root node //create a parent node
noraiz:= xdoc.DocumentElement;
nopai := xdoc.CreateElement('usuario');
TDOMElement(nopai).SetAttribute(widestring('001'), widestring('你')); // create atributes to parent node
noraiz.Appendchild(nopai); // save parent node //create a child node
nopai := xdoc.CreateElement('nome'); // create a child node
//TDOMElement(nopai).SetAttribute('sexo', 'M'); // create atributes
nofilho := xdoc.CreateTextNode('Fernando'); // insert a value to node
nopai.Appendchild(nofilho); // save node
noraiz.ChildNodes.Item[0].AppendChild(nopai); // insert child node in respective parent node //create a child node
nopai := xdoc.CreateElement('idade'); // create a child node
//TDOMElement(nopai).SetAttribute('ano', '1976'); // create atributes
nofilho := xdoc.CreateTextNode('32'); // insert a value to node
nopai.Appendchild(nofilho); // save node
noraiz.ChildNodes.Item[0].AppendChild(nopai); // insert a childnode in respective parent node writeXMLFile(xDoc,'teste.xml'); // write to XML
Xdoc.free;end; 结果如下:
<?xml version="1.0" encoding="gb2312"?>
<cadastro>
<usuario 001="Äã">
<nome>Fernando</nome>
<idade>32</idade>
</usuario>
</cadastro>
代码如下:
procedure Tfrm_xml.Button2Click(Sender: TObject);
var
xdoc: TXMLDocument; // variable to document
noraiz, nopai, nofilho: TDOMNode; // variable to nodes
begin
//create a document xdoc := TXMLDocument.create;
xdoc.Encoding := 'gb2312'; //create a root node
noraiz := xdoc.CreateElement('cadastro');
Xdoc.Appendchild(noraiz); // save root node //create a parent node
noraiz:= xdoc.DocumentElement;
nopai := xdoc.CreateElement('usuario');
TDOMElement(nopai).SetAttribute(widestring('001'), widestring('你')); // create atributes to parent node
noraiz.Appendchild(nopai); // save parent node //create a child node
nopai := xdoc.CreateElement('nome'); // create a child node
//TDOMElement(nopai).SetAttribute('sexo', 'M'); // create atributes
nofilho := xdoc.CreateTextNode('Fernando'); // insert a value to node
nopai.Appendchild(nofilho); // save node
noraiz.ChildNodes.Item[0].AppendChild(nopai); // insert child node in respective parent node //create a child node
nopai := xdoc.CreateElement('idade'); // create a child node
//TDOMElement(nopai).SetAttribute('ano', '1976'); // create atributes
nofilho := xdoc.CreateTextNode('32'); // insert a value to node
nopai.Appendchild(nofilho); // save node
noraiz.ChildNodes.Item[0].AppendChild(nopai); // insert a childnode in respective parent node writeXMLFile(xDoc,'teste.xml'); // write to XML
Xdoc.free;end; 结果如下:
<?xml version="1.0" encoding="gb2312"?>
<cadastro>
<usuario 001="Äã">
<nome>Fernando</nome>
<idade>32</idade>
</usuario>
</cadastro>
解决方案 »
- fastReport 有没有大写金额的函数啊
- 线程访问数据库的问题
- 让程序在U盘中自动运行
- 如何把数据集的字段加到TreeView的节点上
- 急!delphi中如何将dbgrid,或者DataSet中得到的值取出转化为字符串
- 弟弟姐姐们,小弟为这个问题想了很久,对于我来说很难,这是关于idhttp组件的使上有的问题.请看一看吧.我在线等待.
- 关于数据感知控件中的DataField特性,初学者问题。非常感谢!!
- 怎样判断一个进程长时间没有响应!
- 为什么我的程序在别人机器上运行时,不能打印(用的是网络打印机)???在线等候!!!
- 怎么区分是不是可接键盘输入控件
- delphi treeview求嵌套算法
- dbgrideh中如何控制单元格小数位数
DOM是一个类库的名称
<?xml version="1.0" encoding="gb2312" ?>
- <cadastro>
- <usuario 脛茫="脛茫">
<nome>Fernando</nome>
<idade>32</idade>
</usuario>
</cadastro>
xdoc.Active := True;
xdoc.Encoding := 'GB2312'; //好象是大写吧?!
因为现在想把一些数据写活,写到XML或INI文件里做为配置信息,比如:
代码:hr,名称:鸿瑞 规则:abc
代码:nr,名称:南瑞 规则:aabbcc现在想把他想到XML或INI里,都试了,读写都是乱码。现在实在没办法了,想不到办法,或者有什么别的办法做个配置信息存储也可以谢谢unsigned,帮我再想想办法吧
xdoc := TXMLDocument.create(Self);
不能你这样用,create方法如下:
constructor TDOMDocument.Create;
begin
FNodeType := DOCUMENT_NODE;
FNodeName := '#document';
inherited Create(nil);
FOwnerDocument := Self;
end;
我用base64编码存起来有什么用?
你现在找不到还是请把这个问题放在心里,有空帮我想想办法,好吗?谢谢
WideString('你')并不能正确转换编码,应该这样做:
var
Text1:WideString;
Text2:AnsiString;
begin
Text1 := '你';
Text2 := Text1;//这个时候Text2里才是正确的'你'字。