zatlas1 has asked for the wisdom of the Perl Monks concerning the following question:

I have XML that I manipulate and save.

Among other tags, I may have a <body> tag with simple text in it. XML::Smart insists on encoding that in this way:

<body dt:dt="binary.base64">

which causes an error since dt is not defined and I am not interested in defining it.

Is there a way to tell XML::Smart to NOT encode that tag.

XML::Smart proves to be to smart in this case

Thank you

ZA

  • Comment on XML::Smart how to prevent encoding <body> tag

Replies are listed 'Best First'.
Re: XML::Smart how to prevent encoding <body> tag
by marto (Cardinal) on Aug 20, 2025 at 16:37 UTC

    That module has not been update in over 10 years and has issues, are you married to this or open to alternatives? Can you update your post with an example of the XML you're working with and explain some of the manipulations you need to perform?

      sample XML

      <actions> <emailNotification> <body>Good day, This email is to inform you we did not receive bla bla bla bla Thank you. bla bla bla bla</body> <cc>x@y.com, z@y.com</cc>

      code sample

      { my $isconditional; my $otherwise; for (my $i=0; $i < 1000; $i++) { last if ! exists ($XML->{$type}{actions}{emailNotification +}[$i]{body}); my $body = $XML->{$type}{actions}{emailNotification}[$i]{ +body}->content(); $XML->{$type}{actions}{emailNotification}[$i]{body} = $bod +y; print "DEBUG7 $body\n"; } }

        Not a decent code or data sample, previously. Fixing the XML and running something like your code (neither the XML you posted nor the code cater for $type), I don't see the issue you have reported:

        XML:

        <actions> <emailNotification> <body> Good day, This email is to inform you we did not receive bla bla bla bla Thank you. bla bla bla bla </body> <cc>x@y.com, z@y.com</cc> </emailNotification> </actions>

        code:

        #!/usr/bin/perl use strict; use warnings; use feature 'say'; use local::lib; use XML::Smart; my $XML = XML::Smart->new('junk.xml'); my $isconditional; my $otherwise; for (my $i=0; $i < 1000; $i++){ last if ! exists ($XML->{actions}{emailNotification}[$i]{body}); my $body = $XML->{actions}{emailNotification}[$i]{body}->content( +); $XML->{actions}{emailNotification}[$i]{body} = $body; say '$body: ' . $body; say 'xml body: ' . $XML->{actions}{emailNotification}[$i]{body}; say $XML->data; }

        Output:

        $body: Good day, This email is to inform you we did not receive bla bla bla bla Thank you. bla bla bla bla xml body: Good day, This email is to inform you we did not receive bla bla bla bla Thank you. bla bla bla bla <?xml version="1.0" encoding="UTF-8" ?> <?meta name="GENERATOR" content="XML::Smart/1.78 Perl/5.042000 [linux] +" ?> <actions> <emailNotification> <body> Good day, This email is to inform you we did not receive bla bla bla bla Thank you. bla bla bla bla </body> <cc>x@y.com, z@y.com</cc> </emailNotification> </actions> 1
Re: XML::Smart how to prevent encoding <body> tag
by ikegami (Patriarch) on Aug 20, 2025 at 16:49 UTC

    Please provide a minimal, runnable demonstration.

    (I seem to recall XML::Smart having fundamental design issues similar to those of XML::Simple. A quick look at the docs seem to confirm this. If so, please avoid!)

      the code is an established utility that I wrote and is widely used in the organization. We did not anticipate the issue

      if I do not have a solution in the realm of XML::Smart, I will have to rewrite the whole thing. That means that I will probably have to switch to JSON and Python, which are what the organization standardized on