Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Converting utf8 characters to named and numeric entities

by Adithyakiran.k (Novice)
on Jan 21, 2014 at 10:09 UTC ( #1071439=perlquestion: print w/ replies, xml ) Need Help??
Adithyakiran.k has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need help in writing a perl program to convert an xml file which has utf8 characters to their respective named entities for some files and to their numbered entities for some other files.

for example, below is some text from input file

<element1> <p> Some sample text with “special characters” in the file </p> </element1>

Below is sample output I am looking for Case 1: (Numbered entities)

<element1> <p> Some sample text with &#8220;special characters&#8221; in the file </p> </element1>

Below is sample output I am looking for Case 2: (Named entities)

<element1> <p> Some sample text with &ldquo;special characters&rdquo; in the file </p> </element1>

Thanks in Advance !

Comment on Converting utf8 characters to named and numeric entities
Re: Converting utf8 characters to named and numeric entities
by tobyink (Abbot) on Jan 21, 2014 at 10:16 UTC
Re: Converting utf8 characters to named and numeric entities
by moritz (Cardinal) on Jan 21, 2014 at 14:37 UTC

    Note that the only characters you need to escape in XML are <>& and in attributes also ".

    The rest of the permissible Unicode codepoints can simply be written in the document's character encoding, provided you specify the encoding in the <?xml?> header like this:

    <?xml version="1.0" encoding="UTF-8"?>

    (Or if you leave it blank, the XML standard says it must be UTF-8, but sometimes you can find tools that don't adhere to that standard).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1071439]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2014-12-18 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (68 votes), past polls