Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

perl script to print xml data like this

by veerubiji (Sexton)
on Oct 03, 2011 at 15:39 UTC ( #929367=perlquestion: print w/ replies, xml ) Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

How to print multiple number of trouble code and descripions using perl script.

Comment on perl script to print xml data like this
Re: perl script to print xml data like this
by afresh1 (Hermit) on Oct 04, 2011 at 03:07 UTC

    I'm having a very hard time deciphering your question but I think you (may) want something like this. It does not keep the exact XML formatting but that is something that is lost when XML::Simple parses it.

    use strict; use warnings; use XML::Simple; my $xml = new XML::Simple; my $data = $xml->XMLin( \*DATA ); 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} }; } __DATA__ <xml> Removed by request. </xml>
    l8rZ,
    --
    andrew

      Hi afreshl, thanks for your reply, and sorry for replying on both sites. because i cant post clearly there.

      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.

        The error does not occur with the sample so the actual xml is apparently different than what you provided. To get rid of the sorting error, just don't sort the output.

        14 foreach my $code ( keys %by_code ) {

        If you need the output exactly as it was, you will need to use a solution other than XML::Simple. As I said:

        It does not keep the exact XML formatting but that is something that is lost when XML::Simple parses it.
        l8rZ,
        --
        andrew

          Done.

          l8rZ,
          --
          andrew
    Re: perl script to print xml data like this
    by ym_chaitu (Initiate) on Oct 04, 2011 at 05:56 UTC
      hai i guess you can use the XML::LibXML for this this would be the code
      use XML::LibXML; $parser = XML::LibXML->new(); $serverxml = '1.xml'; my $doc= $parser->parse_file($serverxml); my $root = $doc->getDocumentElement; foreach my $file ($root->findnodes('/xml/DTC')){ $trblcode = $file->findvalue('./TroubleCode'); print "code --> ".$trblcode."\n"; $rem = $file->toString(); print "Rem--> ".$rem."\n"; }

        Hi, Thanks for reply,I tried your code but its doesn't print any thing frist some errors occured, that errors or globle symbol $parser requires explicts package name like that. i cleared by declaring my $parser like that. but still nothing is printing. it shows process started and finished.

    Re: perl script to print xml data like this
    by choroba (Abbot) on Oct 04, 2011 at 09:19 UTC
      As I wrote earlier, I usually use XML::XSH2 to handle XML. To get the desired output, the following lines are enough:
      open EEC3_DTC_Specification_03.20.00_formal.xml; for //DTC/TroubleCode { echo :s 'trouble code: ' text() {"\n"} description: ; ls .. ; }

        Hi choroba, thaks for your reply and I will try like your suggestion, because i dnt know that much about XML::XSH2.

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlquestion [id://929367]
    Approved by keszler
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (9)
    As of 2014-09-22 09:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (185 votes), past polls