I have an XML response as follows which I receive from a third party application on basis of the request URL I hit from my perl script from which I need to extract and print values using Perl script. How do i achieve this?
Response:
<LineAnalyseCheck>
<LineInfo>
<ParameterInfo>
<Parameter>
<DownVal>1</DownVal>
<Name>Volt</Name>
<Unit>V</Unit>
<UpVal>29</UpVal>
</Parameter>
</ParameterInfo>
<LineConfigInfo>
<Type2>non.8b</Type2>
<LineSync>true</LineSync>
</LineConfigInfo>
</LineInfo>
<LineInfo>
<ParameterInfo>
<Parameter>
<DownVal>2</DownVal>
<Name>Volt</Name>
<Unit>V</Unit>
<UpVal>20</UpVal>
</Parameter>
</ParameterInfo>
<LineConfigInfo>
<Type2>non.7b</Type2>
<LineSync>true</LineSync>
</LineConfigInfo>
</LineInfo>
</LineAnalyseCheck>
I am taking the above response received in a content array as follows:
@content_arr = split /\s+</, $resp->content();
Based on this content array i am separating the name value and adding in my template.
sample code:
$resp = $ua->request(
GET 'https://URL?xml=' . &GetRequest($input)
);
@content_arr = split /\s+</, $resp->content();
foreach (@content_arr) {
if (/^Parameter>/) {
my @tmp = splice(@content_arr,$counter,4);
&ParamValues(\@tmp);
} elsif (/^(LineStatusExplanation>)/) {
my ($param,$val, undef) = &GetParVal($_);
if ($TEMPLATE{$param} eq "unknown") {
$TEMPLATE{$param} = "$val\n";
} else {
$TEMPLATE{$param} .= "$val\n";
}
}
## Verhoog de loopteller.
$counter++;
}
&PrintIt($);
######## functions ########
sub GetRequest($) {
//request I hit
}
sub PrintIt ($) {
print <<EOF;
Type2 : $TEMPLATE{Type2}
LineSync : $TEMPLATE{LineSync}
Attenuation : Down: $TEMPLATE{'Volt'}->{DownVal} $TEMPLATE{'Volt'}->{Unit} / Up: $TEMPLATE{'Volt'}->{UpVal} $TEMPLATE{'Volt'}->{Unit}
EOF
}
sub GetParVal($) {
split />|</, shift;
}
I need the output as
#expectedOutput:
Type2 : non.8b
LineSync : true
Attenuation : Down: 1 V / Up: 29 V
Type2 : non.7b
LineSync : true
Attenuation : Down: 2 V / Up: 20 V
I have created a template in my perl script as
my %TEMPLATE = (
'Volt' => { DownVal => 'unknown',
UpVal => 'unknown',
Unit => ''
},
'Type2' => 'unknown',
'LineSync' => 'unknown',
);
I am printing the output as :
print <<EOF;
Type2 : $TEMPLATE{Type2}
LineSync : $TEMPLATE{LineSync}
Attenuation : Down: $TEMPLATE{'Volt'}->{DownVal} $TEMPLATE{'Volt'}->{Unit} / Up: $TEMPLATE{'Volt'}->{UpVal} $TEMPLATE{'Volt'}->{Unit}
EOF
The problem is this is only displaying the second value of the xml response. Not both the values. The output I am getting is only as follows:
#receivedOutput
Type2 : non.7b
LineSync : true
Attenuation : Down: 2 V / Up: 20 V
I need output for both 8b and 7b to be displayed as mentioned in expectedOutput above. How do I achieve this?