Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pull Unicode from XML using Delphi

Status
Not open for further replies.

khovorka

Programmer
Jan 21, 2011
2
US
Hello,

I need to be able to do the following and do not know how to do this. Here are the steps.

1. I receive a small XML file with a few lines like the following: <City><![CDATA[BRASÍLIA]]></City>

I am attempting to ready this in with the accent/diacritical
marks using NativeXML

I then need to take this and using Indy10 pass it out
as Unicode to a MySQL database to save it without
the special characters getting scrambled. My code currently
looks like this: (m_CustomerInfo is a record).

m_CustomerInfo.ShipCity := NodeByName('City').NodeByElementType(xeCData).ValueAsUnicodeString;

I then use this to send to the MySQL database:

function Submit_Customerinfo(URL, CITY: Widestring) : String;
var
MPS : TIdMultiPartFormDataStream;
theRESPONSE : String;

begin
theRESPONSE := 'Error: Not Successful in submitting Customer Information.';
try
MPS := TIdMultiPartFormDataStream.Create;
frmMAIN.HTTP.Request.ContentType := MPS.RequestContentType;
MPS.AddFormField('city', CITY);

MPS.Position := 0;
theRESPONSE := frmMAIN.HTTP.Post(URL, MPS);
finally
MPS.Free;
end;

Submit_Customerinfo := theRESPONSE;
end;

So I call Submit_Customerinfo as shown above with a URL
and the CITY string that I need to properly pass I guess
or should I change the parameters in the function from
WideString as well?

This is what I need help with. If the City name is just plain text it works great, but, not with the accents. It gets scrambled. So everything in the code works properly
except submitting accented characters.

Any help on how to do this or where to find a good article to read that can get me through this is appreciated.

Thanks,
Kim




 
You mean by accented like ' and " (quotes)? Couldn't you just remove those if that is the case?

Something like this function should make it work then.

Code:
function RemoveQuotes(S: string): string;
begin
  while (Pos('''', S) <> 0) or (Pos('"', S) <> 0) do
  begin
    if Pos('''', S) <> 0 then
      Delete(S, Pos('''', S), 1)
    else
      Delete(S, Pos('"', S), 1);
  end;
end;

I hope this helped, if not, tell me what you mean by "accented".

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
no he's talking about the I in this word:

BRASÍLIA

khovorka, what delphi version are you using?

/Daddy


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
To Whosrdaddy,

The version is Delphi 2010 prof.
I have things working in parsing the XML and can see the
Unicode text string properly,
but now have to pass it using Indy10 to a MySQL database
and all I get in the database is a bunch of ????????
So any help in understanding what I have to do.
When I look at the specific XML element I use

m_CustomerInfo.ShipCity := UTF8ToWideString(Root.NodeByName('Shipping').NodeByName('sCity').NodeByElementType(xeCData).ValueAsUnicodeString);
and do a ShowMessage(m_CustomerInfo.ShipCity);
and see what I would hope to and now I
just need to get it to the remote MySQL database.

Thanks.


 
Don't know if this is what you are looking for, but it should allow you to save a string with accents

MyString := char($EF)+char($BB)+char($BF)+UTF8Encode(MyString);
 
khovorka,
you must check what charset the MySQL database uses.
99% chance the DB is in latin hence the conversion ??? due to incompatible conversion between unicode and latin charset.

if you have control over the database change the charset on the database.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top