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.


In reply to How to modify an attribute containing special characters in a xml file? by ankit.tayal560

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.