Reply to "globe" about XML encoding conversion

Source: Internet
Author: User
Tags stringreplace
Problem Source: http://www.cnblogs.com/del/archive/2011/03/24/1994029.html#2059114

Globe has such an XML file:

<xmp> <? XML version = "1.0" encoding = "gb2312"?> <Root> <friend name = "Ten Years"> </friend> <friend name = "has been running"> </friend> <friend name = "too many connections commandid "> </friend> </root> </xmp>

It contains Chinese and Korean characters, and some are hexadecimal and some are decimal. The actual content should be:

<xmp> <? XML version = "1.0" encoding = "gb2312"?> <Root> <friend name = "Ten Years"> </friend> <friend name = "has been running"> </friend> <friend name = "too many connections commandid "> </friend> </root> </xmp>

Below is how to open it and save it as a standard UTF-8 format XMLCode(Use delphixe's latest Regular Expression component ):

Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, regularexpressions; Type tform1 = Class (tform) button1: tbutton; procedure button1click (Sender: tobject); Private function mymatchevaluator (const match: tmatch): string; public end; var form1: tform1; implementation {$ R *. DFM} procedure tform1.button1click (Sender: tobject); const Pattern = '? [0-9a-fa-f] {1, 5}; '; var list: tstringlist; Reg: tregex; Path, tmpname: string; begin with topendialog. create (NiL) Do begin execute; Path: = filename; free; end; If Path = ''then exit; List: = tstringlist. create; List. loadfromfile (PATH); list. text: = stringreplace (list. text, 'gb2312', 'utf-8', [rfignorecase]); Reg: = tregex. create (pattern, [rocompiled]); list. text: = reg. replace (list. text, mymatchevaluator); tmpname: = extractfilename (PATH); Path: = stringreplace (path, tmpname, 'utf8 _ '+ tmpname, [rfignorecase]); text: = path; list. savetofile (path, tencoding. utf8); list. free; end; function tform1.mymatchevaluator (const match: tmatch): string; begin result: = match. groups [1]. value; If match. value [3] = 'X' then result: = '$' + result; Result: = widechar (strtoint (result); end.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.