in reply to XML::Simple parsing

Did you think you could avoid the inevitable? Here is how someone from New Hampshire would solve this using XML::Twig! I guess grantm will have more on the way you use XML::Simple ;--)

XML::Twig is quite appropriate here because it lets you specify quite easily that you only want to process header elements with a specific attribute name, and it will only load the relevant part of the document, instead of the entire thing.

#!/usr/bin/perl -w -l use strict; use XML::Twig; my $name= live_free() or die; my $t= XML::Twig->new( twig_roots => { qq{header[\@name="$name"]} => \&print_content + }, ); $t->parse( \*DATA); sub print_content { my( $t, $header)= @_; print join( "\n", map { $_->text } $header->children); } sub live_free { return shift @ARGV; } __DATA__ <?xml version="1.0"?> <!-- This is XML for the Header Mapping --> <map> <header name="ABCstatus"> <act>WTO</act> </header> <header name="ABCevent"> <act>WTO</act> <act>Call</act> </header> <header name="DEF-Call"> <act>Call</act> </header> <header name="Unknown"> <act>WTO</act> <act>SMS</act> <act>NetSend</act> </header> </map>

Replies are listed 'Best First'.
Re: Re: XML::Simple parsing
by AcidHawk (Vicar) on Oct 23, 2002 at 15:11 UTC

    hmmm..I like the look of this. Unfortunately, I am running Windows and ActiveState and the XML::Twig module I could find is for Linux and Solaris OSes.

    Now if I could find this for W2k..;)

    -----
    Of all the things I've lost in my life, its my mind I miss the most.

      There are many ways to install a module beyond ppm:

      • If you have CPAN (or even better CPANPlus) installed you can use it
      • if you have nmake installed you can follow this link to XML::Twig and download it from CPAN, do the magic incantation :
        tar zxvf XML-Twig-3.08.tar.gz cd XML::Twig-3.08 perl Makefile.PL make make test su (is it called su on windows?) make install
      • as XML::Twig is pure Perl you can also just download it, un-tar it and copy Twig.pm in your Perl tree, in the same directory as XML::Parser (something like C:\Perl\site\lib\xml)