NAME
Mojo::DOM - Minimalistic XML DOM Parser With CSS3 Selectors
SYNOPSIS
use Mojo::DOM;
# Parse
my $dom = Mojo::DOM->new;
$dom->parse('<div><div id="a">A</div><div id="b">B</div></div>');
# Find
my $b = $dom->at('#b');
print $b->text;
# Iterate
$dom->find('div[id]')->each(sub { print shift->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->find('a[href]');
An
E
element with afoo
attribute. E[foo="bar"]
-
my $fields = $dom->find('input[name="foo"]');
An
E
element whosefoo
attribute value is exactly equal tobar
. E[foo^="bar"]
-
my $fields = $dom->find('input[name^="f"]');
An
E
element whosefoo
attribute value begins exactly with the stringbar
. E[foo$="bar"]
-
my $fields = $dom->find('input[name$="o"]');
An
E
element whosefoo
attribute value ends exactly with the stringbar
. E:root
-
my $root = $dom->at(':root');
An
E
element, root of the document. E F
-
my $headlines = $dom->find('div h1');
An
F
element descendant of anE
element. E > F
-
my $headlines = $dom->find('html > body > div > h1');
An
F
element child of anE
element.
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');
Find a single element with CSS3 selectors.
attrs
my $attrs = $dom->attrs;
Element attributes.
children
my $children = $dom->children;
Children of element.
find
my $results = $dom->find('html title');
Find elements with CSS3 selectors.
$dom->find('div')->each(sub { print shift->text });
name
my $name = $dom->name;
$dom = $dom->name('html');
Element name.
namespace
my $namespace = $dom->namespace;
Element namespace.
parent
my $parent = $dom->parent;
Parent of element.
parse
$dom = $dom->parse('<foo bar="baz">test</foo>');
Parse XML document.
replace
$dom = $dom->replace('<div>test</div>');
Replace elements.
replace_content
$dom = $dom->replace_content('test');
Replace element content.
root
my $root = $dom->root;
Find root element.
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.