|
I want to create a xml like <?xml version="1.0" encoding="GB2312"?> <Root> <Customer ID="123456"> <name>english name</name> <chinesename>中文名字</chinesename> </Customer> </Root>
and below is the way I use ixmldocument
myxml2:=TXMLDocument.Create(nil); myxml2.Active:=true; myxml2.Options:=[doNodeAutoCreate,doNodeAutoindent]; myxml2.Encoding:='GB2312'; myxml2.NodeIndentStr:=''; pNode:=myxml2.AddChild('Root'); pnode:=pnode.AddChild('Customer'); pnode.Attributes['ID']:=123456; cNode:=pNode.AddChild('name'); cNode.Text:='english name'; cNode:=pNode.AddChild('chinesename'); cNode.Text:='中文名字';
but i want to use nativexml to create it ,if i set the codepage to 936 ,the chinese text is lost. //write xml with nativexml myxml1 := TNativeXml.CreateName('Root'); try // Add a subnode with name "Customer" with myxml1.Root.NodeNew('Customer') do begin // Add an attribute to this subnode WriteAttributeInteger('ID', 123456); // Add subsubnode WriteString('name', 'english name'); writestring('chinesename','中文名字'); end;
// Save the XML in readable format (so with indents) myxml1.XmlFormat := xfReadable; // Save results to a file //I don't need the indentstring to decrease file size myxml1.IndentString:=''; myxml1.ExternalEncoding:=seAnsi; //don't work myxml1.codepage=936; //when this line work the chinese text lost
myxml1.SaveToFile('e:\temp\test1.xml'); finally myxml1.Free;
I use nativexml 4.02 and delphi xe under winxp.
test unit:
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons;
type TForm1 = class(TForm) BitBtn1: TBitBtn; BitBtn2: TBitBtn; procedure BitBtn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
uses NativeXml,xmlintf,xmldoc;
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject); var myxml1:tnativexml; var myxml2:ixmldocument; pNode,cNode: IXMLNode; begin //write xml with nativexml myxml1 := TNativeXml.CreateName('Root'); try // Add a subnode with name "Customer" with myxml1.Root.NodeNew('Customer') do begin // Add an attribute to this subnode WriteAttributeInteger('ID', 123456); // Add subsubnode WriteString('name', 'english name'); writestring('chinesename','中文名字'); end;
// Save the XML in readable format (so with indents) myxml1.XmlFormat := xfReadable; // Save results to a file //I don't need the indentstring to decrease file size myxml1.IndentString:=''; myxml1.ExternalEncoding:=seAnsi; // myxml1.ExternalEncoding:=seUTF8; // myxml1.ExternalCodepage:=936; //only use this line can set the codepage to 936 and showing <?xml version="1.0" encoding="GB2312"?> otherewise encode="utf-8"
myxml1.SaveToFile('e:\temp\test1.xml'); finally myxml1.Free; end; //write xml with ixmldocument;
myxml2:=TXMLDocument.Create(nil); myxml2.Active:=true; myxml2.Options:=[doNodeAutoCreate,doNodeAutoindent]; myxml2.Encoding:='GB2312'; myxml2.NodeIndentStr:=''; pNode:=myxml2.AddChild('Root'); pnode:=pnode.AddChild('Customer'); pnode.Attributes['ID']:=123456; cNode:=pNode.AddChild('name'); cNode.Text:='english name'; cNode:=pNode.AddChild('chinesename'); cNode.Text:='中文名字';
myxml2.SaveToFile('E:\temp\test2.xml');
end;
end.
|