in reply to convert whole html-files to xml
With HTML::Tiny? HTML::Tiny doesn't seem like an appropriate choice. To begin with, it has no support for parsing HTML!
Here's an example using HTML::HTML5::Parser and HTML::HTML5::Writer; two modules that I wrote, which are available on the CPAN.
#!/usr/bin/env perl use strict; use warnings; use HTML::HTML5::Parser; use HTML::HTML5::Writer qw(DOCTYPE_XHTML1); my $parser = 'HTML::HTML5::Parser'->new; my $writer = 'HTML::HTML5::Writer'->new(markup => 'xhtml', doctype => +DOCTYPE_XHTML1); print $writer->document( $parser->load_html(IO => \*DATA) ); __DATA__ <!doctype html> <HTML LANG="en"> <title>Some HTML</TITLE> <P>Here is some HTML</body>
Adding XML::LibXML::PrettyPrint into the mix allows you to tidy up the XHTML output, nicely indenting the tags:
#!/usr/bin/env perl use strict; use warnings; use HTML::HTML5::Parser; use HTML::HTML5::Writer qw(DOCTYPE_XHTML1); use XML::LibXML::PrettyPrint; my $parser = 'HTML::HTML5::Parser'->new; my $writer = 'HTML::HTML5::Writer'->new(markup => 'xhtml', doctype => +DOCTYPE_XHTML1); my $pp = 'XML::LibXML::PrettyPrint'->new_for_html; print $writer->document( $pp->pretty_print( $parser->load_html(IO => \*DATA), ), ); __DATA__ <!doctype html> <HTML LANG="en"> <title>Some HTML</TITLE> <P>Here is some HTML</body>
Sample output:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w +3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html lang="en" xmlns="http:// +www.w3.org/1999/xhtml"> <head> <title>Some HTML</title> </head> <body> <p>Here is some HTML</p> </body> </html>
(I really need to look at adding a line break after the doctype.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: convert whole html-files to xml
by dschinn1001 (Initiate) on Sep 15, 2013 at 09:23 UTC | |
by tobyink (Canon) on Sep 15, 2013 at 10:17 UTC | |
by dschinn1001 (Initiate) on Sep 15, 2013 at 15:54 UTC | |
by Anonymous Monk on Sep 15, 2013 at 21:28 UTC | |
|