Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors
use Mojo::DOM; # Parse my $dom = Mojo::DOM->new('<div><p id="a">A</p><p id="b">B</p></div>'); # Find say $dom->at('#b')->text; say $dom->find('p')->text; say $dom->find('[id]')->attr('id'); # Walk say $dom->div->p->[0]->text; say $dom->div->children('p')->first->{id}; # Iterate $dom->find('p[id]')->each(sub { say shift->{id} }); # Loop for my $e ($dom->find('p[id]')->each) { say $e->text; } # Modify $dom->div->p->[1]->append('<p id="c">C</p>'); $dom->find(':not(p)')->strip; # Render say "$dom";
Mojo::DOM is a minimalistic and relaxed HTML/XML DOM parser with CSS selector support. It will even try to interpret broken XML, so you should not use it for validation.
Mojo::DOM defaults to HTML semantics, that means all tags and attributes are lowercased and selectors need to be lowercase as well.
my $dom = Mojo::DOM->new('<P ID="greeting">Hi!</P>'); say $dom->at('p')->text; say $dom->p->{id};
If XML processing instructions are found, the parser will automatically switch into XML mode and everything becomes case sensitive.
my $dom = Mojo::DOM->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>'); say $dom->at('P')->text; say $dom->P->{ID};
XML detection can also be disabled with the "xml" method.
# Force XML semantics $dom->xml(1); # Force HTML semantics $dom->xml(0);
Mojo::DOM implements the following methods.
my $dom = Mojo::DOM->new; my $dom = Mojo::DOM->new('<foo bar="baz">test</foo>');
Construct a new array-based Mojo::DOM object and "parse" HTML/XML fragment if necessary.
my $trimmed = $dom->all_text; my $untrimmed = $dom->all_text(0);
Extract all text content from DOM structure, smart whitespace trimming is enabled by default.
# "foo bar baz" $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->all_text; # "foo\nbarbaz\n" $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->all_text(0);
my $collection = $dom->ancestors; my $collection = $dom->ancestors('div');
Find all ancestors of this element matching the CSS selector and return a Mojo::Collection object containing these elements as Mojo::DOM objects. All selectors from Mojo::DOM::CSS are supported.
# List types of ancestor elements say $dom->ancestors->type;
$dom = $dom->append('<p>Hi!</p>');
Append HTML/XML fragment to element.
# "<div><h1>A</h1><h2>B</h2></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->append('<h2>B</h2>')->root;
$dom = $dom->append_content('<p>Hi!</p>');
Append HTML/XML fragment to element content.
# "<div><h1>AB</h1></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->append_content('B')->root;
my $result = $dom->at('html title');
Find first element matching the CSS selector and return it as a Mojo::DOM object or return undef if none could be found. All selectors from Mojo::DOM::CSS are supported.
undef
# Find first element with "svg" namespace definition my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'};
my $attrs = $dom->attr; my $foo = $dom->attr('foo'); $dom = $dom->attr({foo => 'bar'}); $dom = $dom->attr(foo => 'bar');
Element attributes.
# List id attributes say $dom->find('*')->attr('id')->compact;
my $collection = $dom->children; my $collection = $dom->children('div');
Find all children of this element matching the CSS selector and return a Mojo::Collection object containing these elements as Mojo::DOM objects. All selectors from Mojo::DOM::CSS are supported.
# Show type of random child element say $dom->children->shuffle->first->type;
my $xml = $dom->content_xml;
Render content of this element to XML.
# "<b>test</b>" $dom->parse('<div><b>test</b></div>')->div->content_xml;
my $collection = $dom->find('html title');
Find all elements matching the CSS selector and return a Mojo::Collection object containing these elements as Mojo::DOM objects. All selectors from Mojo::DOM::CSS are supported.
# Find a specific element and extract information my $id = $dom->find('div')->[23]{id}; # Extract information from multiple elements my @headers = $dom->find('h1, h2, h3')->text->each; my @links = $dom->find('a[href]')->attr('href')->each;
my $result = $dom->match('html title');
Match the CSS selector against this element and return it as a Mojo::DOM object or return undef if it didn't match. All selectors from Mojo::DOM::CSS are supported.
my $namespace = $dom->namespace;
Find element namespace.
# Find namespace for an element with namespace prefix my $namespace = $dom->at('svg > svg\:circle')->namespace; # Find namespace for an element that may or may not have a namespace prefix my $namespace = $dom->at('svg > circle')->namespace;
my $sibling = $dom->next;
Return Mojo::DOM object for next sibling of element or undef if there are no more siblings.
# "<h2>B</h2>" $dom->parse('<div><h1>A</h1><h2>B</h2></div>')->at('h1')->next;
my $parent = $dom->parent;
Return Mojo::DOM object for parent of element or undef if this element has no parent.
$dom = $dom->parse('<foo bar="baz">test</foo>');
Parse HTML/XML fragment with Mojo::DOM::HTML.
# Parse XML my $dom = Mojo::DOM->new->xml(1)->parse($xml);
$dom = $dom->prepend('<p>Hi!</p>');
Prepend HTML/XML fragment to element.
# "<div><h1>A</h1><h2>B</h2></div>" $dom->parse('<div><h2>B</h2></div>')->at('h2')->prepend('<h1>A</h1>')->root;
$dom = $dom->prepend_content('<p>Hi!</p>');
Prepend HTML/XML fragment to element content.
# "<div><h2>AB</h2></div>" $dom->parse('<div><h2>B</h2></div>')->at('h2')->prepend_content('A')->root;
my $sibling = $dom->previous;
Return Mojo::DOM object for previous sibling of element or undef if there are no more siblings.
# "<h1>A</h1>" $dom->parse('<div><h1>A</h1><h2>B</h2></div>')->at('h2')->previous;
my $parent = $dom->remove;
Remove element and return Mojo::DOM object for parent of element.
# "<div></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->remove;
my $parent = $dom->replace('<div>test</div>');
Replace element with HTML/XML fragment and return Mojo::DOM object for parent of element.
# "<div><h2>B</h2></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->replace('<h2>B</h2>'); # "<div></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->replace('');
$dom = $dom->replace_content('<p>test</p>');
Replace element content with HTML/XML fragment.
# "<div><h1>B</h1></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->replace_content('B')->root; # "<div><h1></h1></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->replace_content('')->root;
my $root = $dom->root;
Return Mojo::DOM object for root node.
my $collection = $dom->siblings; my $collection = $dom->siblings('div');
Find all siblings of this element matching the CSS selector and return a Mojo::Collection object containing these elements as Mojo::DOM objects. All selectors from Mojo::DOM::CSS are supported.
# List types of sibling elements say $dom->siblings->type;
my $parent = $dom->strip;
Remove element while preserving its content and return Mojo::DOM object for parent of element.
# "<div>A</div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->strip;
$dom = $dom->tap(sub {...});
Alias for "tap" in Mojo::Base.
my $trimmed = $dom->text; my $untrimmed = $dom->text(0);
Extract text content from element only (not including child elements), smart whitespace trimming is enabled by default.
# "foo baz" $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->text; # "foo\nbaz\n" $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->text(0);
my $trimmed = $dom->text_after; my $untrimmed = $dom->text_after(0);
Extract text content immediately following element, smart whitespace trimming is enabled by default.
# "baz" $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_after; # "baz\n" $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_after(0);
my $trimmed = $dom->text_before; my $untrimmed = $dom->text_before(0);
Extract text content immediately preceding element, smart whitespace trimming is enabled by default.
# "foo" $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_before; # "foo\n" $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_before(0);
my $xml = $dom->to_xml; my $xml = "$dom";
Render this element and its content to XML.
# "<b>test</b>" $dom->parse('<div><b>test</b></div>')->div->b->to_xml;
my $tree = $dom->tree; $dom = $dom->tree(['root', ['text', 'foo']]);
Document Object Model. Note that this structure should only be used very carefully since it is very dynamic.
my $type = $dom->type; $dom = $dom->type('div');
Element type.
# List types of child elements say $dom->children->type;
my $bool = $dom->xml; $dom = $dom->xml($bool);
Disable HTML semantics in parser and activate case sensitivity, defaults to auto detection based on processing instructions.
In addition to the methods above, many child elements are also automatically available as object methods, which return a Mojo::DOM or Mojo::Collection object, depending on number of children.
say $dom->p->text; say $dom->div->[23]->text; say $dom->div->text;
Direct hash reference access to element attributes is also possible.
say $dom->{foo}; say $dom->div->{id};
Mojolicious, Mojolicious::Guides, http://mojolicio.us.
To install Mojolicious, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious
CPAN shell
perl -MCPAN -e shell install Mojolicious
For more information on module installation, please visit the detailed CPAN module installation guide.