ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; use Data::Dumper; use XML::DOM; my $parser=new XML::DOM::Parser; my $doc = $parser->parsefile('C:\perl\perl_tests\package.xml')or die$! +; my $root=$doc->getDocumentElement(); my $string="Johnson Controls Automotive Electronics&#xA;Package: GMLAN + 3.1 - Single Channel&#xA;Micro: uPD70F3524&#xA;Compiler: Green Hills + 5.1.7-CBD1110030"; my @package=$root->getElementsByTagName("package"); foreach my $package(@package) { if($package->getAttribute("name")) { my $name=$package->getAttribute("name"); print("$name\n"); $package->setAttribute("name",$string); my $name_updated=$package->getAttribute("name"); print("$name_updated\n"); } } $doc->setXMLDecl($doc->createXMLDecl('1.0','UTF-8')); $doc->printToFile("C:/perl/perl_tests/package.xml"); XML FILE : <?xml version="1.0" encoding="UTF-8"?> <ecuconfig id="1" name="ECU: IPC_LS" FrameworkVersion="1.4.48.0" Compa +tibleVersion="1.4.0.0"> <package name="Johnson Controls Automotive Electronics Package: GMLAN +3.1 - Single Channel Micro: uPD70F3524 Compiler: Green Hills 5.1.7-CB +D1110030" path="C:\Vector\CBD1110030_D04_V85x\Generators\Components\" +> <delivery version="09.01.22.01.11.00.30.04.00.00"/> </package> <package> <delivery version="08.00.09.01.01.00.04.00.00.00"/> </package> </ecuconfig> MODIFIED XML FILE AFTER RUNNING THIS SCRIPT : <?xml version="1.0" encoding="UTF-8"?> <ecuconfig id="1" name="ECU: IPC_LS" FrameworkVersion="1.4.48.0" Compa +tibleVersion="1.4.0.0"> <package name="Johnson Controls Automotive Electronics&amp;#xA;Package +: GMLAN 3.1 - Single Channel&amp;#xA;Micro: uPD70F3524&amp;#xA;Compil +er: Green Hills 5.1.7-CBD1110030" path="C:\Vector\CBD1110030_D04_V85x +\Generators\Components\"> <delivery version="09.01.22.01.11.00.30.04.00.00"/> </package> <package> <delivery version="08.00.09.01.01.00.04.00.00.00"/> </package> </ecuconfig>

I've written above script to change the name attribute of package element as shown in $string but it is giving me some weird modified xml file idk why.! pretty confused.! can someone help me out with this? although the printed results in the cmd prompt are correct according to my script.

  • Comment on How to modify an attribute containing special characters in a xml file?
  • Download Code

Replies are listed 'Best First'.
Re: How to modify an attribute containing special characters in a xml file?
by Corion (Patriarch) on Sep 30, 2016 at 10:34 UTC

    What do you expect?

    & is a special character in XML and a literal & needs to be encoded as &amp; in XML. Maybe you did not want &#xA in your original string but something else?

      ok got your point but I want that expression only in my string . any suggestions regarding how should I proceed??

        You will need to learn about ampersand-encoding and XML then. I guess that &#xA; is already encoded and the &#xA; stands for the character \x0A in Perl parlance. So you will need to change your initial string to:

        my $string="Johnson Controls Automotive Electronics\x0APackage: GMLAN +3.1 - Single Channel\x0AMicro: uPD70F3524\x0ACompiler: Green Hills 5. +1.7-BD1110030";

        or, in a more sensible fashion:

        my $string= join "\n", "Johnson Controls Automotive Electronics", "Package: GMLAN 3.1 - Single Channel", "Micro: uPD70F3524", "Compiler: Green Hills 5.1.7-BD1110030";

        I guess that the original text contains line breaks and you want to preserve them.

Re: How to modify an attribute containing special characters in a xml file?
by jellisii2 (Hermit) on Sep 30, 2016 at 12:06 UTC
Re: How to modify an attribute containing special characters in a xml file?
by kcott (Archbishop) on Sep 30, 2016 at 11:12 UTC

      already tried that. don't change a thing ! tried quoting & as \& also but dint worked!

        "... ! tried quoting & as \& also but dint worked!"

        Firstly, see ++Corion's response to you in this thread (to which you replied "ok got your point ...").

        The code I posted in "Syntax-highlight Non-Perl Code for HTML" has, near the start:

        { my %entity_for = qw{& &amp; < &lt; > &gt;}; sub chars_to_ents { $_[0] =~ s/([&<>])/$entity_for{$1}/gr } }

        You'll find usage near the end of the script. Perhaps you could write somethng like this tailored to your needs.

        — Ken