1

How to print multiple number of trouble code and descripions using perl script.I have the xml file like this..

 <data>........
        .......
      </data>

lik this i have more number of trouble codes.still now i am printing only trouble code,how should i print descrption below the trouble code.

3 Answers 3

4

Try this:

#!/C:/Languages/Perl64/bin/perl.exe

use warnings;
use strict;
use XML::LibXML::Reader;

my $file;
open( $file, 'test.xml' );
my $reader = XML::LibXML::Reader->new( IO => $file ) or die( "unable to open file" );
while ( $reader->nextElement( 'DTC' ) ) {
    my $description = $reader->readOuterXml();
    $reader->nextElement( 'TroubleCode' );
    my $troubleCode = $reader->readInnerXml();
    print( "trouble code: $troubleCode\n" );
    print( " description: $description\n" );
}
close( $file );

Okay, corrected and tested. This works per your question.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Lucas, Thank you very much for your reply,I tried with your code its working like same as i want to print.@lucas
Can i print this data in table like all trouble code in one column and their descriptions in other column.
@viswa - accept this as your answer and then ask a new question for your new problem.
Hi lucas, thank you very much for helping. i got my answer from your code.@lucas
@viswa You are very welcome, though it does not appear to be marked as the answer. Make sure you click the check mark next to the answer to mark it as such.
1

I answered the same question over on perlmonks so hopefully he finds an answer he likes.

use XML::Simple;
my $xml = new XML::Simple;

my $data = $xml->XMLin("data.xml");

my %by_code;
foreach my $dtc ( @{ $data->{DTC} } ) {
    push @{ $by_code{ $dtc->{TroubleCode} } }, $dtc;
}

foreach my $code ( sort { $a <=> $b } keys %by_code ) {
    print "trouble code: $code\n";
    print "description:\n";
    print map { $xml->XMLout( $_, RootName => 'DTC', NoAttr => 1, ) }
        @{ $by_code{$code} };
}

3 Comments

Hi, Thank you very much for helping, <code> {Argument "'\x{32}\x{30}..." isn't numeric in sort }"at foreach my $code (sort....) line. i am getting this argument in th second for loop frist line and your code is working but i am getting XML data under description is not like original format, it displying in diffrent order bottom comments to up and upper comments to down.@AFresh1
@viswa Correct, as I said on Perlmonks, XML::Simple does not keep the original formatting of the XML. The sorting errors are caused by there being non-numeric data in the TroubleCodes. Either you need to clean that or don't sort it numerically.
Thank you very much. i got answer from lucas I tried and modified. i am happy now.@afresh.
0

Would you try this:

 print " DTCnumber: \n" . join("\n", @dtcnumber);

and:

print " DTCdes: \n" .  join("\n", @dtcdes);

1 Comment

Thanks for reply, i tried but its not working for displying description, it displays only trouble codes may be problem with code.@ sergio

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.