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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|