in reply to Pasing an XML file to generate a certain output.

Why not just use a real XML parser?

use XML::Twig; # at most one div will be loaded in memory my $twig=XML::Twig->new( twig_handlers => { channel => sub { my( $node )= @_; my( $id )= $node->{'att'}->{id}; my( $chan )= $node->{'att'}->{chan}; my( $sign )= $node->text; say qq(UPDATE channel SET channum="$chan",xmltvid="$id" WHERE +callsign="$sign"); $node->purge; }, }, ); $twig->parsefile( 'my_big.xml');

Replies are listed 'Best First'.
Re^2: Pasing an XML file to generate a certain output.
by Discipulus (Canon) on May 13, 2015 at 08:22 UTC
    eh, i was trying the same solution but with the code below (your's) and with my attempt too i get not well-formed (invalid token) at line 1, column 29, byte 29 at.. tht seems to be the comment.
    If i entirely remove the comment it runs fine. i'm playng with comments => "process" as the authors said, but with no luck.

    #!/usr/bin/perl use XML::Twig; # at most one div will be loaded in memory my $twig=XML::Twig->new( twig_handlers => { channel => sub { my( $node )= @_; my( $id )= $node->{'att'}->{id}; my( $chan )= $node->{'att'}->{chan}; my( $sign )= $node->text; print qq(UPDATE channel SET channum="$chan",xmltvid="$id" WHER +E callsign="$sign"); $node->purge; }, }, ); $twig->parse(<DATA>); __DATA__ <channel id="6945.dvb.guide" <!-- number="84" type="0x19" flags="0xf" +bouquet="4110" region="2000000e" sid="6945" --> > <display-name>6945</display-name> </channel>

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      If i entirely remove the comment it runs fine. i'm playng with comments => "process" as the authors said, but with no luck.

      Hmm, xml parsers are required to reject invalid xml -- comments inside tags are invalid xml :)

      I wouldn't play anymore :D

        Ah ok thanks, it appeared to me as a very bad thing having a comment inside a tag, but i was not aware it was not valid XML.

        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        Hmmm,

        What if I remove all the rubbish I dont need from the file, how would I parse it then.
        http://paste.ubuntu.com/11112412/
Re^2: Pasing an XML file to generate a certain output.
by Saner (Novice) on May 13, 2015 at 08:24 UTC
    Thanks for the speedy reply!
    I am new to this and had not heard of this method, I had tried a few others, and someone suggested perl so I got caught up in regexs and stuff. I didnt even know of this method.
    I feel I am close with this, but I get an error on line 11.
    perl xml2 syntax error at xml2 line 11, near "say qq(UPDATE channel SET channum= +"$chan",xmltvid="$id" WHERE callsign="$sign")" Execution of xml2 aborted due to compilation errors.
    Any pointers ?
      try adding use feature qw/ say /;
Re^2: Pasing an XML file to generate a certain output.
by Saner (Novice) on May 14, 2015 at 06:56 UTC
    Cheers everyone, I managed between all the help here to get it working. Its not the most elegant solution (I strip out all the junk and then parse it) but it works.
    As to where the file came from, it is a dump from SkyUK's OpenTV system. by default (by design i think) I can only get Now / Next , using this setup I can get a full 7 days on my PVR.
    Thanks once again for qll your input.