Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Get a structured tally of XML tags

by graff (Chancellor)
on Apr 24, 2009 at 01:42 UTC ( [id://759715]=CUFP: print w/replies, xml ) Need Help??

Get a quick snapshot of the quantity and structure of elements in an XML file -- useful for getting acquainted with some new XML source, or for confirming expectations about XML data you created. (Yes, it is worthwhile to use XML::Parser in a one-liner...)
perl -MXML::Parser -le '$p=XML::Parser->new( Handlers=>{Start=>sub{$k.=".$_[1]";$h{$k}++}, End=>sub{$k=~s/.$_[1]$//;}}); $p->parsefile(shift); END{print "$h{$_}\t$_" for (sort keys %h)}' some_file.xml

(Line-breaks added for legibility; the first time I did this, it really was all on one line -- honest!)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 18:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found