Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: XML Examples (and a call for participation)

by PodMaster (Abbot)
on Nov 19, 2002 at 14:41 UTC ( [id://214129]=note: print w/replies, xml ) Need Help??


in reply to XML Examples (and a call for participation)

I love XML::Twig, but here's what I still do with HTML, and did with XML before I learned about XML::Twig ;)
use XML::TokeParser; my $file = 'REC-xml-19980210.xml'; my $i = 0; my $p = XML::TokeParser->new($file); my $Ret = ""; while(defined(my $t = $p->get_token() )){ if($t->[0] eq 'S' and $t->[1] eq 'lhs') { $i++; $Ret = join '', "[$i] ", $p->get_text('/lhs'), " ::= "; }elsif( $t->[0] eq 'S' and $t->[1] eq 'rhs'){ $Ret .= $p->get_text('/rhs'); }elsif($t->[0] eq 'E' and $t->[1] eq 'prod'){ print clean($Ret),"\n"; $Ret = ""; } } undef $Ret; undef $p; ## mirod already did this, so I'm borrowing sub prod { my( $twig, $prod)= @_; my $lhs= $prod->field( 'lhs'); my $rhs= join '', map {$_->text} $prod->children( 'rhs'); $i++; my $prod_text = "[$i] $lhs ::= $rhs"; print clean( $prod_text) . "\n"; } sub clean { my( $string)= @_; $string =~ s/\xc2\xa0/ /sg; $string =~ s/\s+/ /g; $string=~ s{\s$}{}g; return $string; }

____________________________________________________
** The Third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: Re: XML Examples (and a call for participation)
by mirod (Canon) on Nov 20, 2002 at 11:52 UTC

    Excellent, PodMaster++.

    I did not even know that XML::TokeParser existed, but the code is pretty simple, I like it.

    The code (minus the prod function which is not needed) has been added to the article. That's now a lot more than one way to do it (17!)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://214129]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-16 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found