http://qs321.pair.com?node_id=517041


in reply to XML::LibXML getElementsById problem

Wow, thank god for XML::TreeBuilder
my $tree = XML::TreeBuilder->new_from_content(<<EOF); ?xml version="1.0"?> <root> <aaa id='test'> <bbb/> </aaa> </root> EOF my $elem = $tree->look_down(id => 'text'); $elem->this; $elem->that;

Replies are listed 'Best First'.
Re^2: XML::LibXML getElementsById problem
by mirod (Canon) on Dec 15, 2005 at 19:19 UTC

    Oh, if we are pimping alternate modules, then of course id IS magical in XML::Twig:

    #!/usr/bin/perl -w use strict; use XML::Twig; my $xml_string = <<EOF; <?xml version="1.0"?> <root> <aaa id='test'> <bbb/> </aaa> </root> EOF my $t= XML::Twig->nparse( $xml_string); my $elem= $t->getElementById( 'test'); $elem->print;

    And of course you can use it to process HTML too (it sub-contracts the HTML to XHTML conversion to HTML::TreeBuilder):

    #!/usr/bin/perl -w use strict; use XML::Twig; my $html_string = <<EOF; <html> <head><title>Just a quick example</title></head> <body><h1>Example</h1> an example<p> <div id="test">gotcha!</div> <hr> </html> EOF my $t= XML::Twig->new->parse_html( $html_string); my $elem= $t->getElementById( 'test'); $elem->print;