Question

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.

Was it helpful?

Solution

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";

OTHER TIPS

The Result nodes have no text content. They only have attributes and attribute values.

Text content is what goes between beginning and ending tags.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top