#!/usr/bin/perl -w # # Copyright (c) 2001 Dominic Mitchell. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the # distribution. # # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. # # Show the structure of an XML document - tags only. # XXX Doesn't bloody work with unknown entities. # # @(#) $Id: xmlstruct.pl,v 1.2 2001/07/30 21:47:18 dom Exp $ # use strict; use vars qw(%opt $indent $incr); use Getopt::Std; use XML::Parser; my $ic; # Indent Character. sub print_start_tag { my $tag = shift; my @attrs = @_; print $ic x $indent, "<", $tag; while (@attrs) { my $key = shift @attrs; my $val = shift @attrs; print " ", $key, "='", $val, "'"; } print ">\n"; } sub print_end_tag { my $tag = shift; print $ic x $indent, "\n"; } sub start_tag { my $expat = shift; my $tag = shift; my @attrs = @_; print_start_tag($tag, @attrs) if $opt{p} && $tag =~ m/$opt{p}/io; $indent += $incr; } sub end_tag { my $expat = shift; my $tag = shift; my @attrs = @_; $indent -= $incr; print_end_tag($tag) if $opt{p} && $tag =~ m/$opt{p}/io; } sub comment { my $expat = shift; my $data = shift; print $ic x $indent, "\n"; } # -i: Specify amount to indent by. # -p: Pass in a pattern. Only print tags that match pattern. getopts("c:i:p:", \%opt); $incr = $opt{i} || 2; # How much to increase the indent by. $indent = 0; # How much we are currently indented by. $opt{p} ||= "."; # Default tag pattern. $ic = $opt{c} || " "; # What char to indent with. my $p = new XML::Parser(Handlers => {Start => \&start_tag, Comment => \&comment, End => \&end_tag}); # Default to stdin. eval { $p->parsefile($ARGV[0] || "-"); }; if ($@) { $@ =~ s/ at \/.*//; die $@; }