我用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>

解决方案 »

  1.   

    用的什么XML操作库?要处理好乱码的问题,个人推荐使用MSXML.
      

  2.   

    我用的是DOM,我要在win和linux下跑,暂时只发现可以用DOM
      

  3.   

    DOM应该不是某一个库的名称吧...没用过。一般跨平台的库,除非做得比较大,自己包含了相应的编码映射,否则是无法识别编码的。另外还有一个问题,就是你是使用什么打开来看的,建议使用浏览器看是不是乱码。
      

  4.   

    我是程序打开的,没有用浏览器
    DOM是一个类库的名称
      

  5.   

    用网页打开也是:
    <?xml version="1.0" encoding="gb2312" ?> 
    - <cadastro>
    - <usuario 脛茫="脛茫">
      <nome>Fernando</nome> 
      <idade>32</idade> 
      </usuario>
      </cadastro>
      

  6.   

    另外,你在linux下跑用的也是Kylix编译的吗?如果不是,那跟用不用DOM有什么关系?
      

  7.   

    那你不使用gb2312,换用utf-8有什么问题吗?
      

  8.   

    好像有一个俄罗斯写的simeXML这个处理可以吧,具体我也没用过.
      

  9.   

      xdoc := TXMLDocument.create; 
      xdoc.Active := True;
      xdoc.Encoding := 'GB2312'; //好象是大写吧?!
      

  10.   

    我现在使用的是一个开源的OmniXML,也不支持gb2312(它的编码表里面没有)只能自己修改并添加进去。另外就是有众多的BUG,现在已经被我改得面目全非,也算凑合着能用。
      

  11.   

    unsigned :我都试了,不行,我是用的lazarus,现在是在winxp下编译运行,有这个问题。要换别的xml类库,我不知道lazarus可不可以用
      

  12.   

    那个DOM,看了半天,任何中文都是乱码,不知道如何处理的,跟不进去。
      

  13.   

    谢谢unsigned ,帮我看了半天,太谢谢你了
    因为现在想把一些数据写活,写到XML或INI文件里做为配置信息,比如:
    代码:hr,名称:鸿瑞 规则:abc
    代码:nr,名称:南瑞 规则:aabbcc现在想把他想到XML或INI里,都试了,读写都是乱码。现在实在没办法了,想不到办法,或者有什么别的办法做个配置信息存储也可以谢谢unsigned,帮我再想想办法吧
      

  14.   

    //试试,我以前碰到过这个问题
    xdoc := TXMLDocument.create(Self); 
      

  15.   

    刚又玩了一下lazarus,很让人寒心的是,什么属性编辑器,点那个+号展开子项就有BUG,一个OpenDialog,打开以后,鼠标在上面移动两下又出错,然后程序就挂了...
      

  16.   


    不能你这样用,create方法如下:
    constructor TDOMDocument.Create;
    begin
      FNodeType := DOCUMENT_NODE;
      FNodeName := '#document';
      inherited Create(nil);
      FOwnerDocument := Self;
    end;  
      

  17.   

    实在不行,就先base64编码存着吧。一时间我也不知道有什么支持lazarus又好用的XML库
      

  18.   


    我用base64编码存起来有什么用?
    你现在找不到还是请把这个问题放在心里,有空帮我想想办法,好吗?谢谢
      

  19.   

    其实是AnsiString和WideString的转换问题。
    WideString('你')并不能正确转换编码,应该这样做:
    var
      Text1:WideString;
      Text2:AnsiString;
    begin
      Text1 := '你';
      Text2 := Text1;//这个时候Text2里才是正确的'你'字。