I am using perl's XML::LibXML module to parse an XML response from a device. It appears that the only way I can successfully get my data is by modifying the XML response from the device. Here is my XML response from the device:
<chassis-inventory xmlns="http://xml.juniper.net/junos/10.3D0/junos-chassis">
<chassis junosstyle="inventory">
<name>Chassis</name>
<serial-number>JN111863EAFF</serial-number>
<description>VJX1000</description>
<chassis-module>
<name>Midplane</name>
</chassis-module>
<chassis-module>
<name>System IO</name>
</chassis-module>
<chassis-module>
<name>Routing Engine</name>
<description>VJX1000</description>
<chassis-re-disk-module>
<name>ad0</name>
<disk-size>1953</disk-size>
<model>QEMU HARDDISK</model>
<serial-number>QM00001</serial-number>
<description>Hard Disk</description>
</chassis-re-disk-module>
</chassis-module>
<chassis-module>
<name>FPC 0</name>
<chassis-sub-module>
<name>PIC 0</name>
</chassis-sub-module>
</chassis-module>
<chassis-module>
<name>Power Supply 0</name>
</chassis-module>
</chassis>
</chassis-inventory>
Here is the perl code I am using to parse and find the serial number for example:
#!/bin/env perl
use strict;
use warnings;
use XML::LibXML;
my $f = ("/var/working/xmlstuff");
sub yeah {
my $ff;
my $f = shift;
open(my $fff,$f);
while(<$fff>) {
$_ =~ s/^\s+$//;
$_ =~ s/^(<\S+)\s.*?=.*?((?:\/)?>)/$1$2/g;
$ff .= $_;
}
close($fff);
return $ff
}
my $tparse = XML::LibXML->new();
my $ss = $tparse->load_xml( string => &yeah($f));
print map $_->to_literal,$ss->findnodes('/chassis-inventory/chassis/serial-number');
If I do not use the regex substitution nothing is loaded for the script to parse. I can understand the stripping of newlines, but why do I have to remove the attributes from the XML response, so it only works if these lines:
<chassis-inventory xmlns="http://xml.juniper.net/junos/10.3D0/junos-chassis">
<chassis junosstyle="inventory">
Become this:
<chassis-inventory>
<chassis>
Is this a problem with the XML response or with the XML::LibXML module?
Is there a way to have it ignore the fact that there is empty lines in the file without using a regex substitution?
Thanks for the help.