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!
Delphi

Avatar of undefined
Last Comment
flasht
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;
Avatar of flasht
flasht

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>
Avatar of flasht
flasht

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Hardi
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.
Avatar of flasht
flasht

As far as i know it should.
Avatar of Hardi
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
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
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
Avatar of flasht
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
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!
Avatar of flasht
flasht

You're welcome :)
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo