Skip Menu |

This queue is for tickets about the XML-Entities CPAN distribution.

Report information
The Basics
Id: 78744
Status: resolved
Priority: 0/
Queue: XML-Entities

People
Owner: Sixtease+cpan [...] gmail.com
Requestors: NHORNE [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: encode
Please add XML::Entities::encode(), which would be similar to HTML::Entities::encode(). Thanks.
You can use HTML::Entities::encode to encode all XML entities that are defined in XML::Entities module. I added the documentation, it will show up in a few hours as a new version. I'll paste it here as well: The HTML::Entities module provides a function for encoding entities. You just have to assign the right mapping to the C<%HTML::Entities::char2entity> hash. So, to encode everything that XML::Entities knows about, you'd say: use XML::Entities; use HTML::Entities; %HTML::Entities::char2entity = %{ XML::Entities::Data::char2entity('all'); }; my $encoded = encode_entities('tom&jerry'); # now $encoded is 'tom&amp;jerry'
Subject: Re: [rt.cpan.org #78744] encode
Date: Fri, 03 Aug 2012 14:40:39 +0100
To: bug-XML-Entities [...] rt.cpan.org
From: Nigel Horne <njh [...] bandsman.co.uk>
On 03/08/12 12:42, Oldrich Kruza via RT wrote: Show quoted text
> use XML::Entities; > use HTML::Entities; > %HTML::Entities::char2entity = %{ > XML::Entities::Data::char2entity('all'); > }; > my $encoded = encode_entities('tom&jerry'); > # now $encoded is 'tom&amp;jerry'
Thanks. That's getting close, but I have a scenario which reads HTML data and wants to send it as XML as demonstrated by this code: #!/usr/bin/perl -wT use XML::Entities; use HTML::Entities; my $string = 'PROVENCE-ALPES-C&Ocirc;TE d\'AZUR'; $string = decode_entities($string); %HTML::Entities::char2entity = %{ XML::Entities::Data::char2entity('all'); }; print encode_entities($string), "\n"; This outputs as PROVENCE-ALPES-C&Ocirc;TE d&apos;AZUR Which I believe is not valid XML. I believe what I want is: PROVENCE-ALPES-C&O#212;TE d&#130;AZUR Am I right? I admit I'm unsure on this.
HTML::Entities has an encode_numeric function that does exactly that. See http://search.cpan.org/perldoc?HTML%3A%3AEntities
Fantastic - thanks! That does the trick.