Reputation: 107
I want to parse following XML file using Perl script.
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="generate_report.xslt"?>
<TestRun>
<ConfigFile cfg="NA"/>
<version ver="1.05.00"/>
<date date="15:25:53 14/05/2014: "/>
<TestName TN="ABC service test"/>
<SerialPort serialPortNumber="NA"/>
<TimeOut TOInSec="60"/>
<Protocol prot="CAN"/>
<DeviceName CanUSBDev="CANpro USB"/>
<BaudRateCAN BR="500 kBaud"/>
<BoardNodeNr BNNR="NA"/>
<BoardNVID BNVID="NA"/>
<PCNodeNr PCNNR="NA"/>
<PCNVID PCNVID="NA"/>
<TCResult>
<Result TestNr="1" Type="STEP" StepNr="1" Message="no msg required" Result="FAILED"/>
<Result TestNr="1" Type="STEP" StepNr="2" Message="no msg required" Result="PASSED"/>
<Result TestNr="1" Type="CASE" Result="FAILED"/>
</TCResult>
</TestRun>
and my script is as follows
use strict;
use warnings;
# Import the XML::LibXML module
use XML::LibXML;
#my $LogXML = XMLin($ARGV[0], ForceArray => 1);
my $Parser = XML::LibXML->new();
my $XMLDoc = $Parser->parse_file($ARGV[0]);
for my $sample ( $XMLDoc->findnodes('/TestRun/TCResult'))
{
foreach my $child ( $sample->findnodes('*'))
{
print "\t", $child->nodeName(), ":", $child->textContent(), "\n";
}
}
after execution I get output as
Result: Result: Result:
only and not all text content.
Please advice.
Upvotes: 2
Views: 148
Reputation: 359
Sounds like you want to get the attributes. Add the following inside the foreach loop:
foreach my $at ($child->attributes())
{
print $at->name() . ": " . $at->value() . "\t";
}
print "\n";
Upvotes: 1
Reputation: 164
The Result nodes have no text content. They only have attributes and attribute values.
Text content is what goes between beginning and ending tags.
Upvotes: 0