Adrian Adrian -4 years ago 98
Perl Question

XMLin removes the CDATA

I am reading an XML file with XML::Simple ( XMLin), doing some substitute operations in some of it's attributes and then XMLOut it in another file. What I noticed is that some of the attributes contained

before and after XMLOut they don't anymore.

Input example:
<name><![CDATA[some text here]]></name>

Output :
<name>some text here</name>

Is there an option to keep the CDATA attr? ( I know what CDATA stands for and why it's used)

Answer Source

The fact that the text was provided via a CDATA section is lost during parsing. Furthermore, there XML::Simple never produces CDATA sections.

Note that the two XML documents you presented are 100% equivalent. But if you absolutely want to preserve the CDATA sections, I recommend switching to XML::LibXML[1].

$ perl -MXML::LibXML -e'
   my $xml = "<name><![CDATA[some text here]]></name>";
<?xml version="1.0"?>
<name><![CDATA[some text here]]></name>

The conversion should be relatively simple since both XML::Simple and XML::LibXML provide functionally similar interfaces. For example,

  • my $val = $node->{attr};my $val = $node->getAttribute('attr')
  • $node->{attr} = $val;$node->setAttribute('attr', $val)
  • for (@$node)for ($node->getChildren())

  1. I recommend switching no matter what. It'll make your life so much simpler!
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download