in reply to XML - Escaping characters from database for XML

For example, XML::LibXML:
use strict; use warnings; use feature 'say'; use XML::LibXML; my $xml = XML::LibXML->createDocument(); my $r = $xml->createElement('root'); $xml->setDocumentElement($r); $r->addChild($xml->createTextNode('<&')); say $xml->toString;
Or its wrapper, XML::XSH2:
create root ; insert text '<&' into /root ; ls . ;

Replies are listed 'Best First'.
Re^2: XML - Escaping characters from database for XML
by tobyink (Canon) on May 28, 2012 at 14:34 UTC

    With XML::LibXML, you can get away without creating a document and element...

    print XML::LibXML::Text->new('<&')->toString;

    It's pretty easy to wrap that up into a function:

    sub escape_xml { XML::LibXML::Text->new(shift)->toString } print escape_xml('<&');
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Well, yes. I was just discreetly suggesting creating the whole packages in XML::LibXML.