How can I escape text for an XML document in Perl?

前端 未结 9 1186
长发绾君心
长发绾君心 2021-01-01 06:53

Anyone know of any Perl module to escape text in an XML document?

I\'m generating XML which will contain text that was entered by the user. I want to correctly handl

9条回答
  •  猫巷女王i
    2021-01-01 07:05

    I personally prefer XML::LibXML - Perl binding for libxml. One of the pros - it uses one of the fastest XML processing library available. Here is an example for creating text node:

    use XML::LibXML;
    my $doc = XML::LibXML::Document->new('1.0',$some_encoding);
    my $element = $doc->createElement($name);
    $element->appendText($text);
    $xml_fragment = $element->toString();
    $xml_document = $doc->toString();
    

    And, never, ever create XML by hand. It's gonna be bad for your health when people find out what you did.

提交回复
热议问题