Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Answer: Strip that XML!

by mirod (Canon)
on Dec 14, 2000 at 12:00 UTC ( #46592=categorized answer: print w/replies, xml ) Need Help??

Q&A > regular expressions > Strip that XML! - Answer contributed by mirod

Using XML::Parser (basic mode, no style):

#!/bin/perl -w use strict; use XML::Parser; my @email; my @name; my $stored_content = ''; # global used to store text sub start { my( $expat, $gi, %atts ) = @_; $stored_content=''; # reset } sub char { my( $expat, $string ) = @_; $stored_content .= $string; # accumulate } sub end { my( $expat, $gi ) = @_; # now we can do some "real" processing with the element content push @name, $stored_content if( $gi eq 'name'); push @email, $stored_content if( $gi eq 'email'); $stored_content = ''; # reset } # create the parser my $parser = new XML::Parser( Handlers => { Start => \&start, # called for each start tag Char => \&char, # called for all text (including \n between tags +) End => \&end # called for each end tag } ); my $xml = <<EOF; <xml> <email></email><name>Toto</name> <email></email><name>Tata</name> <email></email><name>Tutu</name> </xml> EOF $parser->parse( $xml ); print "email: @email\n"; print "name: @name\n";

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2017-12-16 05:39 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (448 votes). Check out past polls.