SimDesign

SimDesign software users forum
It is currently Sun May 26, 2013 3:22 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: help on create ansi xml
PostPosted: Tue May 22, 2012 6:35 pm 
Offline
Junior Member

Joined: Thu Jun 24, 2010 9:36 am
Posts: 3
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.


Top
 Profile  
 
PostPosted: Sun May 27, 2012 4:53 pm 
Offline
Junior Member

Joined: Thu Jun 24, 2010 9:36 am
Posts: 3
pls tell me wether there is a way to correct this problem?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group