XML pretty printing in HTML

Warren Hedley w.hedley at auckland.ac.nz
Wed Jul 7 23:14:13 BST 1999


Hi Matt

Thanks for your help with this problem. I've been playing around with
your code and it's pretty impressive (in its elegance, I mean). I haven't
had much of a chance to look at XML::Parser so far, but this really
blew me away.

Anyway, because of problems with empty elements (generating start and
end events) and whitespace handling, I may have to write a regexp-based
parser as you suggested.

As it happens, this wasn't anything close to legal HTML4. You're not
allowed to but <FONT> elements inside a <PRE> section anyway. So I'm
going to have to mess around with <P>'s, <BR />'s and &nbsp;'s.

Sounds like fun.


Matt Sergeant wrote:
> 
> OK. Here's an attempt:
> 
> use XML::Parser;
> 
> print XML::Parser->new(Handlers => {
>         # Handlers using closures, except Start 'cos it's more complex.
>         # $_[0] is the expat object where we store the HTML output
>         Init => sub { $_[0]->{html} = '<pre style="font:12pt monospace">' },
>         Final => sub { $_[0]->{html} .= </pre>; return($_[0]->{html}) },
>         Start => \&start,
>         End => sub { $_[0]->{html} .= '&lt;/<font color="green">' . $_[1] .
> '</font>&gt;' },
>         Char => sub { $_[0]->{html} .= "<b>$_[1]</b>" },
>         CdataStart => sub { $_[0]->{html} .= '<font color="yellow">' },
>         CdataEnd => sub { $_[0]->{html} .= '</font>' },
>         }
> )->parsefile($ARGV[0]);
> 
> sub start {
>         my $expat = shift;
>         my $element = shift;
>         my %attribs = @_;
> 
>         $expat->{html} .= '&lt;<font color="green">' . $element . '</font>';
>         if (%attribs) {
>                 foreach (keys %attribs) {
>                         $expat->{html} .= ' <font color="red">' . $_ .
>                                 '</font>=&quot;<font color="blue">' .
>                                 $attribs{$_}.
>                                 '</font>&quot;';
>                 }
>         }
>         $expat->{html} .= '&gt;';
> }




-- 
Warren Hedley

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To (un)subscribe, mailto:majordomo at ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)





More information about the Xml-dev mailing list