0

If I'm on a system with just XML::Simple installed, and I'm reading a database to output an XML representation of some of it, and some of that data has HTML code which needs to be surrounded by the CDATA tag... is that possible?

I really don't want to jump through the necessary hoops to install a new module, but if it's impossible with XML::Simple, which module would you recommend?

Dan Goodspeed
  • 3,484
  • 4
  • 26
  • 35
  • 3
    XML::LibXML and XML::Twig are popular choices, and both give you far more control than XML::Simple – TobyLL Feb 24 '15 at 09:20
  • 4
    Are you sure you need to output the CDATA sections? For any (sane) XML processor, whether the content is in a CDATA section or is encoded using entities should make no difference at all. – mirod Feb 24 '15 at 09:32
  • @mirod - I asked the company that will be doing the reading of the outputted XML file about some of the nodes having HTML content in it and they said "just make sure the contents of the node are wrapped in CDATA. That way we can import them as-is"... so that's where I am. Maybe they can handle it encoded. I should ask. Thanks. – Dan Goodspeed Feb 24 '15 at 10:13
  • *“I really don't want to jump through the necessary hoops to install a new module”* I suspect that you simply haven't installed a module before. It should be as simple as `cpan install XML::Twig` on the command line. – Borodin Feb 24 '15 at 15:54
  • 1
    XML::Simple is aweful for reading XML. XML::Simple is completely useless at writing XML (to be read by anything other than XML::Simple). – ikegami Feb 24 '15 at 16:41
  • Actually, it's as simple as `cpan XML::Twig`. – ikegami Feb 24 '15 at 17:04
  • @Borodin - I tried the cpan install and it ran for about 10 minutes but finished with the line "make test had returned bad status, won't install without force". – Dan Goodspeed Feb 24 '15 at 18:11
  • @DanGoodspeed: We can't tell what went wrong without seeing the full build log. Can you put it up on [`pastebin`](http://pastebin.com/) and post a link to it? – Borodin Feb 24 '15 at 18:21
  • @Borodin - [Done](http://pastebin.com/2hR8NU9r). – Dan Goodspeed Feb 24 '15 at 18:58
  • You might find there's a prebuilt package for your OS. Your version of perl is quite an old one though. Looks like it might not be fetching prereqs? – Sobrique Feb 24 '15 at 19:13
  • Okay that's not a problem. Only two tests failed out of 107. The first is to do with handling the HTML entity `&Amp;` (which should be `&amp`) and the second is about round-trip processing of XML data with a `DOCTYPE` instruction. You should be okay installing with those problems, as long as you keep them in mind. Just `cpan -f XML::Twig` will *force* the installation and you'll be up and running. – Borodin Feb 24 '15 at 19:30
  • @Borodin So I just did `cpan -f XML::Twig` like you said to force the install, and at the end of the process it still said "make test had returned bad status, won't install without force", and didn't install. Is there a different way to force-install it? – Dan Goodspeed Feb 27 '15 at 07:24

1 Answers1

1

This will be a little incomplete. Because it would rely on your input/output specifics.

I like XML::Twig as an alternative to XML::Simple. Within the docs for XML::Simple you have:

The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces.

Within the documentation for XML::Twig you have (read above link for more, this is reproduced for convenience):

To create an element foo containing a CDATA section:

my $foo= XML::Twig::Elt->new( '#CDATA' => "content of the CDATA section")
                              ->wrap_in( 'foo');

An attribute of '#CDATA', will create the content of the element as CDATA:

my $elt= XML::Twig::Elt->new( 'p' => { '#CDATA' => 1}, 'foo < bar');

creates an element

<p><![CDATA[foo < bar]]></>

This I think should encompass your needs?

Sobrique
  • 52,974
  • 7
  • 60
  • 101