Connecting Tech Pros Worldwide Forums | Help | Site Map

XML::Simple output issue

Newbie
 
Join Date: Mar 2007
Posts: 4
#1: Mar 8 '08
Hi,

I'm attempting to create a perl script that will modify a series of RRD databases (a couple hundred of them). in order to do this the RRD database can be exported to XML modified and then restored from XML.

My issue is when using XML::Simple to read in and edit the xml structure and then output it back out the section of XML that was
previously:
Expand|Select|Wrap|Line Numbers
  1. <cdp_prep>                         
  2.    <ds>
  3.       <primary_value>8.2156307858e+03</primary_value>
  4.       <secondary_value>8.2156307858e+03</secondary_value>
  5.       <value> NaN </value>
  6.       <unknown_datapoints> 0 </unknown_datapoints>
  7.    </ds>
  8. </cdp_prep> 
  9.  
becomes:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <cdp_prep>       
  3.    <name>ds</name>       
  4.    <primary_value> 8.2156307858e+03 </primary_value>      
  5.    <secondary_value> 8.2156307858e+03 </secondary_value>
  6.    <unknown_datapoints> 0 </unknown_datapoints>       
  7.    <value> NaN </value>     
  8. </cdp_prep> 
  9.  
the ds tags are no longer there and are instead turned into a new tag called name with a value of ds.

here's a bit of the perl that's doing the altering of the xml files

Expand|Select|Wrap|Line Numbers
  1.  
  2. # read XML file     
  3. my $data = $xml->XMLin("dump.xml");      
  4. $data->{step} = 600;      
  5.  
  6. my @xml;
  7.  
  8. for my $rra ( @{ $data->{rra} } ) { 
  9.    my $lcv=0;         
  10.    my @new;         
  11.  
  12.    for my $value ( @{ $rra->{database}->{row} } ) {
  13.       if($lcv % 2 == 0 || $lcv == 0) {                 
  14.          push(@new, \%$value);                      
  15.       }             
  16.       $lcv++;         
  17.    }             
  18.  
  19.    $rra->{database}->{row} = \@new;             
  20.    push(@xml, $rra);     
  21. }          
  22.  
  23. $data->{rra} = \@xml;     
  24.  
  25. XMLout($data,     OutputFile => $file.".xml",    RootName => 'rrd',  NoAttr => 1);
  26.  
I've been looking around for a while now and don't understand what the problem is. There is however another ds tag higher up in the hierarchy...

Original XML:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <ds>                 
  3.    <name> ds0 </name>                 
  4.    <type> DERIVE </type>                 
  5.    <minimal_heartbeat> 900 </minimal_heartbeat>                 
  6.    <min> 0.0000000000e+00 </min>                 
  7.    <max> NaN </max>               
  8.  
  9.    <!-- PDP Status -->                 
  10.    <last_ds> 1808321630 </last_ds>                 
  11.    <value> 3.3702190024e+06 </value>                 
  12.    <unknown_sec> 0 </unknown_sec>         
  13. </ds>  
  14.  
  15. <!-- Round Robin Archives -->   
  16. <rra>                 
  17.    <cf> AVERAGE </cf>                 
  18.    <pdp_per_row> 1 </pdp_per_row> <!-- 300 seconds -->        
  19.    <params>                 
  20.       <xff> 5.0000000000e-01 </xff>                 
  21.    </params>                
  22.    <cdp_prep>                         
  23.       <ds>                         
  24.          <primary_value> 8.2156307858e+03 </primary_value>
  25.          <secondary_value> 8.2156307858e+03 </secondary_value>
  26.          <value> NaN </value>
  27.          <unknown_datapoints> 0 </unknown_datapoints> 
  28.       </ds>
  29.    </cdp_prep> 
  30.  ... etc
  31.  

I could use any help or suggestions or reading materials anyone could suggest on this.

Thanks

Moderator
 
Join Date: Mar 2006
Posts: 1,103
#2: Mar 10 '08

re: XML::Simple output issue


If you rename the ds tag to say dt, would it then work?
It's not a good idea to have multiple ds tags which represent different things unless they're in different namespaces.
Newbie
 
Join Date: Mar 2007
Posts: 4
#3: Mar 10 '08

re: XML::Simple output issue


Thanks for the suggestion.... but that also doesn't seem to work. i changed the DS tag to DT and it also gets placed in a tag called NAME...

I wonder if this is a bug in/with XML::Simple perl module... I'm not extremely family with XML so I'm not sure of any best practices or rules but I'm not creating the original XML structure so all i can do is alter it at this point... I'll keep looking but if there are any other suggestions i would be glad to hear them
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#4: Mar 11 '08

re: XML::Simple output issue


Took me several hours to install simpleXML for perl, but found the answer:
Use the KeyAttr parameter to stop this from happening.


XMLout($data, OutputFile => "beer.xml", RootName => 'root', KeyAttr => [], NoAttr => 1);

http://rohan.almeida.in/archives/xml...ons-for-xmlout
Newbie
 
Join Date: Mar 2007
Posts: 4
#5: Mar 11 '08

re: XML::Simple output issue


WOW! thanks a LOT... i looked at those config options and didn't think it applied to this so i skipped right over it. sweet!

I still hit another wall (even w/ the correct XML output) the rrd files wont restore but i believe that's an issues to take up with the rrd mailing list because the XML is correct now.

The only difference between the original and the modified XML now is that the modified doesn't have comments and the order of the tags are sorted alphabetically ... i dont see why either of those would cause a problem but i'll talk to them to see if those are the cause (or i'll do some testing myself)


THANKS AGAIN!
Newbie
 
Join Date: Mar 2007
Posts: 4
#6: Mar 12 '08

re: XML::Simple output issue


I figured i would update this just in case anyone else was interested. It turns out that the RRD files were element order dependent, and XML::Simple does not preserve the element order...

So long story short i had to scrap the XML::Simple and switch to XML::LibXML which is a bit more complicated to use at first but turns out to work very well.
Reply