NAME
Mojo::DOM - Minimalistic XML DOM Parser With CSS3 Selectors
SYNOPSIS
use Mojo::DOM;
my $dom = Mojo::DOM->new;
$dom->parse('<div><div id="a">A</div><div id="b">B</div></div>');
my $b = $dom->at('#b');
print $b->text;
DESCRIPTION
Mojo::DOM is a minimalistic and very relaxed XML DOM parser with support for CSS3 selectors. Note that this module is EXPERIMENTAL and might change without warning!
SELECTORS
These CSS3 selectors are currently implemented.
*-
Any element.
E-
my $title = $dom->at('title');An element of type
E. E[foo]-
my $links = $dom->search('a[href]');An
Eelement with afooattribute. E[foo="bar"]-
my $fields = $dom->search('input[name="foo"]');An
Eelement whosefooattribute value is exactly equal tobar. E[foo^="bar"]-
my $fields = $dom->search('input[name^="f"]');An
Eelement whosefooattribute value begins exactly with the stringbar. E[foo$="bar"]-
my $fields = $dom->search('input[name$="o"]');An
Eelement whosefooattribute value ends exactly with the stringbar. E:root-
my $root = $dom->at(':root');An
Eelement, root of the document. E F-
my $headlines = $dom->search('div h1');An
Felement descendant of anEelement. E > F-
my $headlines = $dom->search('html > body > div > h1');An
Felement child of anEelement.
ATTRIBUTES
Mojo::DOM implements the following attributes.
charset
my $charset = $dom->charset;
$dom = $dom->charset('UTF-8');
Charset used for decoding XML.
tree
my $array = $dom->tree;
$dom = $dom->tree(['root', ['text', 'lalala']]);
Document Object Model.
METHODS
Mojo::DOM inherits all methods from Mojo::Base and implements the following new ones.
all_text
my $text = $dom->all_text;
Extract all text content from DOM structure.
at
my $result = $dom->at('html title');
Search for a single element with CSS3 selectors.
attributes
my $attrs = $dom->attributes;
Element attributes.
children
my $children = $dom->children;
Children of element.
name
my $name = $dom->name;
$dom = $dom->name('html');
Element name.
parent
my $parent = $dom->parent;
Parent of element.
parse
$dom = $dom->parse('<foo bar="baz">test</foo>');
Parse XML document.
search
my $results = $dom->search('html title');
Search for elements with CSS3 selectors.
text
my $text = $dom->text;
Extract text content from element only, not including child elements.
to_xml
my $xml = $dom->to_xml;
Render DOM to XML.