Link to home
Start Free TrialLog in
Avatar of Hardi
Hardi

asked on

Using Open XML to edit Unicode XML files

I need help from someone who has used Open XML to read and write Unicode XML files, with Delphi 7.

I just installed Open XML (http://www.philo.de/xml/)
and TntWare (http://www.tntware.com/delphicontrols/unicode/)
to read, edit, and save XML files.
However I am not sure how to use these components for working with Unicode strings.

Could someone give me a sample working code to use those 2 components to do the following:
- Read an XML file (the encoding can be anything, mostly UTF-8 though)
- Display some value (can be non-ASCII) in a TntEdit (I would probably use a stringgrid later, but I think TntEdit is the simplest for just an example). This value can then be edited.
- Save the XML without changing the encoding, updating the value that has been edited

Let's say I expect the XML to be in this format:
<?xml version="1.0" encoding="......." ?>
<blah>
   <item name="....." description="....." />
</blah>

I want to be able to edit the value of description, where it may contain any character (eg. Chinese or anything)

Thanks a lot!
Avatar of flasht
flasht

Use XMLDocument component from Internet tab...

XML.LoadFromFile('c:\test.xml');
XML.Active := True;

ShowMessage(XML.DocumentElement.LocalName);

var
  i: Integer;
begin
  for i := 0 to XML.DocumentElement.ChildNodes.Count - 1 do
    Memo1.Lines.Add(XML.DocumentElement.ChildNodes[i].LocalName);
end;
Oh... And...

<?xml version="1.0" encoding="UTF-8" ?>
<blah>
   <item name="myname" description="whatever" />
</blah>

Is not a valid XML code...

It should be:

<?xml version="1.0" encoding="UTF-8" ?>
<blah>
   <item name="myname">

   </item>
</blah>
<?xml version="1.0" encoding="UTF-8" ?>
<blah>
   <item name="myname">
      <description> blablabla </description>
   </item>
</blah>
ASKER CERTIFIED SOLUTION
Avatar of flasht
flasht

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hardi

ASKER

Yup, the <item> has a description attribute which can be asian characters and that's what I want to read, display and edit.
Does that really handle asian characters? I thought Delphi doesn't really support Unicode?
Anyway, I'll try it, thanks.
As far as i know it should.
Avatar of Hardi

ASKER

Wow it works. Thanks a lot flasht!
How can I uninstall Open XML components now ^^;
Btw can I change the encoding of the XML? eg. whatever the input encoding is, i wanna output a UTF-8 XML file.
Avatar of Hardi

ASKER

flasht, sorry could you help me again?
If I want to copy the item
<item name="myname2" description="whatever"/>
(the real thing has a lot of attributes)
changing the name to "myname3", inserting it just below myname2, how can I do it?
Avatar of Hardi

ASKER

I found out about changing the encoding, just change xml.Encoding isn't it?
I also found out about reading all attributes using AttributeNodes, so I can add a child node and assign all the attributes. But is there any easier way?
And if you know... is there a way to uninstall installed components?
Thanks flasht
To uninstall components go to Components > Install Packages... Find the package with components you want to uninstall and just remove it.

Yes... xml.encoding is for changing the encoding :)

If you want to change node order you can use .ReplaceNode function... but you still have to construct whole node because you have to give a node as an argument.
Avatar of Hardi

ASKER

I see... great! I think I can use CloneNode with it. Well I'll play around with them later.
Thanks a lot flasht!
You're welcome :)