kmmmf kmmmf - 7 months ago 10
Perl Question

perl code to update attributes in xml with out affecting the xml format

I want to update attributes in xml using perl.The problem here is when I am updaing the attributes of xml it is happening but the xml format is being changed.Breaking my head but no use !

Can anyone pls suggest me some perl code to update attributes in xml with out affecting the xml format

I used the perl code as shown below

#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;

my $xml_file = '3.xml';

my $xml = XMLin(
$xml_file,
KeepRoot => 1,
ForceArray => 1
);

$xml->{outer1}->[0]->{inner1}->[1]->{name}->[0]->{first} = 'Shane Bond';

XMLout(
$xml,
KeepRoot => 1,
NoAttr => 1,
OutputFile => $xml_file,
);


Input xml:

<outer1>
<inner1>
<name>Stonecold</name>
<org>wwf</org>
<profession>
<Bowler>hai</Bowler>
</profession>
</inner1>
<inner1>
<name first = "Shanebond" />
<org>newzealand</org>
<profession>Shane Bond</profession>
</inner1>
<inner1>
<name>brain schemidit</name>
<org>Google</org>
<profession>Chairman</profession>
</inner1>
</outer1>


Expected Output xml:

<outer1>
<inner1>
<name>Stonecold</name>
<org>wwf</org>
<profession>
<Bowler>hai</Bowler>
</profession>
</inner1>
<inner1>
<name first = "Shane Bond" />
<org>newzealand</org>
<profession>Shane Bond</profession>
</inner1>
<inner1>
<name>brain schemidit</name>
<org>Google</org>
<profession>Chairman</profession>
</inner1>
</outer1>


Actual Output xml:

<outer1>
<inner1>
<name>Stonecold</name>
<org>wwf</org>
<profession>
<Bowler>hai</Bowler>
</profession>
</inner1>
<inner1>
<name>
<first>Shane Bond</first>
</name>
<org>newzealand</org>
<profession>Shane Bond</profession>
</inner1>
<inner1>
<name>brain schemidit</name>
<org>Google</org>
<profession>Chairman</profession>
</inner1>
</outer1>

Answer

You set NoAttr => 1 for XMLout(). The XML::Simple documentation says:

NoAttr => 1 # in+out - handy

When used with XMLout(), the generated XML will contain no attributes. All hash key/values will be represented as nested elements instead.

When used with XMLin(), any attributes in the XML will be ignored.

You want an attribute, but turn attributes off?

I tried: print XMLout( XMLin("t.xml", KeepRoot => 1, ForceArray => 1), KeepRoot => 1 );

The diff of both files looks ok:

$ diff -bBEup t.xml t2.xml 
--- t.xml   2016-04-29 10:36:28.446578760 +0200
+++ t2.xml  2016-04-29 10:39:03.450073658 +0200
@@ -7,7 +7,7 @@
     </profession>
   </inner1>
   <inner1>
-     <name first = "Shanebond" />
+    <name first="Shanebond" />
      <org>newzealand</org>
      <profession>Shane Bond</profession>
   </inner1>

Everything looks ok without NoAttr:

$x = XMLin("t.xml", KeepRoot => 1, ForceArray => 1);
$x->{outer1}->[0]->{inner1}->[1]->{name}->[0]->{first} = "Larry";
print XMLout($x, KeepRoot => 1);

<outer1>
  <inner1>
    <name>Stonecold</name>
    <org>wwf</org>
    <profession>
      <Bowler>hai</Bowler>
    </profession>
  </inner1>
  <inner1>
    <name first="Larry" />
    <org>newzealand</org>
    <profession>Shane Bond</profession>
  </inner1>
  <inner1>
    <name>brain schemidit</name>
    <org>Google</org>
    <profession>Chairman</profession>
  </inner1>
</outer1>
Comments