29
29
A StreamParser provides a progressive parse of its input. As each Element is completed, it is emitted via a Stream or
30
30
Iterator interface. Elements returned will be complete with all their children, and an (empty) next sibling, if
31
31
applicable.
32
- <p>Elements (or their children) may be removed from the DOM during the parse, for e.g. to conserve memory, providing a
33
- mechanism to parse an input document that would otherwise be too large to fit into memory, yet still providing a DOM
34
- interface to the document and its elements.</p>
32
+ <p>To conserve memory, you can {@link Node#remove() remove()} Elements (or their children) from the DOM during the
33
+ parse. This provides a mechanism to parse an input document that would otherwise be too large to fit into memory, yet
34
+ still providing a DOM interface to the document and its elements.</p>
35
35
<p>
36
36
Additionally, the parser provides a {@link #selectFirst(String query)} / {@link #selectNext(String query)}, which will
37
37
run the parser until a hit is found, at which point the parse is suspended. It can be resumed via another
@@ -45,6 +45,8 @@ interface to the document and its elements.</p>
45
45
New parsers should be used in each thread.</p>
46
46
<p>If created via {@link Connection.Response#streamParser()}, or another Reader that is I/O backed, the iterator and
47
47
stream consumers will throw an {@link java.io.UncheckedIOException} if the underlying Reader errors during read.</p>
48
+ <p>For examples, see the jsoup
49
+ <a href="https://jsoup.org/cookbook/input/streamparser-dom-sax">StreamParser cookbook.</a></p>
48
50
@since 1.18.1
49
51
*/
50
52
public class StreamParser implements Closeable {
@@ -204,6 +206,7 @@ public List<Node> completeFragment() throws IOException {
204
206
@param query the {@link org.jsoup.select.Selector} query.
205
207
@return the first matching {@link Element}, or {@code null} if there's no match
206
208
@throws IOException if an I/O error occurs
209
+ @see #selectFirst(Evaluator)
207
210
*/
208
211
public @ Nullable Element selectFirst (String query ) throws IOException {
209
212
return selectFirst (QueryParser .parse (query ));
@@ -228,9 +231,12 @@ public Element expectFirst(String query) throws IOException {
228
231
/**
229
232
Finds the first Element that matches the provided query. If the parsed Document does not already have a match, the
230
233
input will be parsed until the first match is found, or the input is completely read.
234
+ <p>By providing a compiled evaluator vs a CSS selector, this method may be more efficient when executing the same
235
+ query against multiple documents.</p>
231
236
@param eval the {@link org.jsoup.select.Selector} evaluator.
232
237
@return the first matching {@link Element}, or {@code null} if there's no match
233
238
@throws IOException if an I/O error occurs
239
+ @see QueryParser#parse(String)
234
240
*/
235
241
public @ Nullable Element selectFirst (Evaluator eval ) throws IOException {
236
242
final Document doc = document ();
@@ -248,6 +254,7 @@ public Element expectFirst(String query) throws IOException {
248
254
@param query the {@link org.jsoup.select.Selector} query.
249
255
@return the next matching {@link Element}, or {@code null} if there's no match
250
256
@throws IOException if an I/O error occurs
257
+ @see #selectNext(Evaluator)
251
258
*/
252
259
public @ Nullable Element selectNext (String query ) throws IOException {
253
260
return selectNext (QueryParser .parse (query ));
@@ -272,9 +279,12 @@ public Element expectNext(String query) throws IOException {
272
279
/**
273
280
Finds the next Element that matches the provided query. The input will be parsed until the next match is found, or
274
281
the input is completely read.
282
+ <p>By providing a compiled evaluator vs a CSS selector, this method may be more efficient when executing the same
283
+ query against multiple documents.</p>
275
284
@param eval the {@link org.jsoup.select.Selector} evaluator.
276
285
@return the next matching {@link Element}, or {@code null} if there's no match
277
286
@throws IOException if an I/O error occurs
287
+ @see QueryParser#parse(String)
278
288
*/
279
289
public @ Nullable Element selectNext (Evaluator eval ) throws IOException {
280
290
try {
0 commit comments