It's not HTML. HTML doesn't even have namespaces. It claims to be XHTML, so you should be using parse_file.
Unfortunately, it's not well-formed XHTML. Lots of unescaped ampersands and less-than symbols, for starters.
The following fixes the escaping errors, but there's still a bunch of duplicate ids and unmatched tags:
#!/usr/bin/perl -w use strict; use warnings; use XML::LibXML qw( ); use LWP::UserAgent qw( ); sub text_to_xml { my ($text) = @_; $text =~ s/&/&/g; $text =~ s/</</g; return $text; } my $url = "http://www.marketwatch.com/story/brazil-mexico-stocks-face- +quarterly-slides-2010-06-30?siteid=rss&rss=1"; my $ua = LWP::UserAgent->new(); my $response = $ua->get($url); $response->is_success() or die "Request failed: ". $response->status_line() ."\n"; my $xhtml = $response->decoded_content(charset => "none"); $xhtml =~ s{(<script[^>]*>)(.*?)(</script>)}{"$1".text_to_xml("$2")."$ +3"}sieg; $xhtml =~ s/&(?!#|[a-zA-Z0-9]{1,8};)/&/g; my $parser = XML::LibXML->new; my $doc = $parser->parse_string($xhtml);
There's a parser option that directs libxml to recover from errors if possible. It might help. If not, HTML::Parser is designed to handle garbage.
In reply to Re: Namespace error while parsing html document.
by ikegami
in thread Namespace error while parsing html document.
by mr_p
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |