1Mojo::DOM(3) User Contributed Perl Documentation Mojo::DOM(3)
2
3
4
6 Mojo::DOM - Minimalistic XML DOM Parser With CSS3 Selectors
7
9 use Mojo::DOM;
10
11 # Parse
12 my $dom = Mojo::DOM->new;
13 $dom->parse('<div><div id="a">A</div><div id="b">B</div></div>');
14
15 # Find
16 my $b = $dom->at('#b');
17 print $b->text;
18
19 # Iterate
20 $dom->find('div[id]')->each(sub { print shift->text });
21
23 Mojo::DOM is a minimalistic and very relaxed XML DOM parser with
24 support for CSS3 selectors. Note that this module is EXPERIMENTAL and
25 might change without warning!
26
27 SELECTORS
28 These CSS3 selectors are currently implemented.
29
30 "*" Any element.
31
32 "E"
33 my $title = $dom->at('title');
34
35 An element of type "E".
36
37 "E[foo]"
38 my $links = $dom->find('a[href]');
39
40 An "E" element with a "foo" attribute.
41
42 "E[foo="bar"]"
43 my $fields = $dom->find('input[name="foo"]');
44
45 An "E" element whose "foo" attribute value is exactly equal to
46 "bar".
47
48 "E[foo^="bar"]"
49 my $fields = $dom->find('input[name^="f"]');
50
51 An "E" element whose "foo" attribute value begins exactly with the
52 string "bar".
53
54 "E[foo$="bar"]"
55 my $fields = $dom->find('input[name$="o"]');
56
57 An "E" element whose "foo" attribute value ends exactly with the
58 string "bar".
59
60 "E:root"
61 my $root = $dom->at(':root');
62
63 An "E" element, root of the document.
64
65 "E F"
66 my $headlines = $dom->find('div h1');
67
68 An "F" element descendant of an "E" element.
69
70 "E > F"
71 my $headlines = $dom->find('html > body > div > h1');
72
73 An "F" element child of an "E" element.
74
76 Mojo::DOM implements the following attributes.
77
78 "charset"
79 my $charset = $dom->charset;
80 $dom = $dom->charset('UTF-8');
81
82 Charset used for decoding XML.
83
84 "tree"
85 my $array = $dom->tree;
86 $dom = $dom->tree(['root', ['text', 'lalala']]);
87
88 Document Object Model.
89
91 Mojo::DOM inherits all methods from Mojo::Base and implements the
92 following new ones.
93
94 "all_text"
95 my $text = $dom->all_text;
96
97 Extract all text content from DOM structure.
98
99 "at"
100 my $result = $dom->at('html title');
101
102 Find a single element with CSS3 selectors.
103
104 "attrs"
105 my $attrs = $dom->attrs;
106
107 Element attributes.
108
109 "children"
110 my $children = $dom->children;
111
112 Children of element.
113
114 "find"
115 my $results = $dom->find('html title');
116
117 Find elements with CSS3 selectors.
118
119 $dom->find('div')->each(sub { print shift->text });
120
121 "name"
122 my $name = $dom->name;
123 $dom = $dom->name('html');
124
125 Element name.
126
127 "namespace"
128 my $namespace = $dom->namespace;
129
130 Element namespace.
131
132 "parent"
133 my $parent = $dom->parent;
134
135 Parent of element.
136
137 "parse"
138 $dom = $dom->parse('<foo bar="baz">test</foo>');
139
140 Parse XML document.
141
142 "replace"
143 $dom = $dom->replace('<div>test</div>');
144
145 Replace elements.
146
147 "replace_content"
148 $dom = $dom->replace_content('test');
149
150 Replace element content.
151
152 "root"
153 my $root = $dom->root;
154
155 Find root element.
156
157 "text"
158 my $text = $dom->text;
159
160 Extract text content from element only, not including child elements.
161
162 "to_xml"
163 my $xml = $dom->to_xml;
164
165 Render DOM to XML.
166
168 Mojolicious, Mojolicious::Guides, <http://mojolicious.org>.
169
170
171
172perl v5.12.3 2010-08-16 Mojo::DOM(3)