/** * Get the <i>nth</i> matched element as an Elements object. * <p> * See also {@link #get(int)} to retrieve an Element. * @param index the (zero-based) index of the element in the list to retain * @return Elements containing only the specified element, or, if that element did not exist, an empty list. */ public Elements eq(int index) { return size() > index ? new Elements(get(index)) : new Elements(); }
/** * Get this element's parent and ancestors, up to the document root. * @return this element's stack of parents, closest first. */ public Elements parents() { Elements parents = new Elements(); accumulateParents(this, parents); return parents; }
/** * Get this element's child elements. * <p> * This is effectively a filter on {@link #childNodes()} to get Element nodes. * </p> * @return child elements. If this element has no children, returns an empty list. * @see #childNodes() */ public Elements children() { return new Elements(childElementsList()); }
/** * Get all of the parents and ancestor elements of the matched elements. * @return all of the parents and ancestor elements of the matched elements */ public Elements parents() { HashSet<Element> combo = new LinkedHashSet<>(); for (Element e: this) { combo.addAll(e.parents()); } return new Elements(combo); }
/** * Creates a deep copy of these elements. * @return a deep copy */ @Override public Elements clone() { Elements clone = new Elements(size()); for(Element e : this) clone.add(e.clone()); return clone; }
/** * Get sibling elements. If the element has no sibling elements, returns an empty list. An element is not a sibling * of itself, so will not be included in the returned list. * @return sibling elements */ public Elements siblingElements() { if (parentNode == null) return new Elements(0); List<Element> elements = parent().childElementsList(); Elements siblings = new Elements(elements.size() - 1); for (Element el: elements) if (el != this) siblings.add(el); return siblings; }
/** Build a list of elements, by visiting root and every descendant of root, and testing it against the evaluator. @param eval Evaluator to test elements against @param root root of tree to descend @return list of matches; empty if none */ public static Elements collect (Evaluator eval, Element root) { Elements elements = new Elements(); NodeTraversor.traverse(new Accumulator(root, elements, eval), root); return elements; }
static Elements filterOut(Collection<Element> elements, Collection<Element> outs) { Elements output = new Elements(); for (Element el : elements) { boolean found = false; for (Element out : outs) { if (el.equals(out)) { found = true; break; } } if (!found) output.add(el); } return output; }
/** * Find elements matching selector. * * @param query CSS selector * @param roots root elements to descend into * @return matching elements, empty if none */ public static Elements select(String query, Iterable<Element> roots) { Validate.notEmpty(query); Validate.notNull(roots); Evaluator evaluator = QueryParser.parse(query); ArrayList<Element> elements = new ArrayList<>(); IdentityHashMap<Element, Boolean> seenElements = new IdentityHashMap<>(); // dedupe elements by identity, not equality for (Element root : roots) { final Elements found = select(evaluator, root); for (Element el : found) { if (!seenElements.containsKey(el)) { elements.add(el); seenElements.put(el, Boolean.TRUE); } } } return new Elements(elements); }
String rawUrl = "https://500px.com" + photo.getString("url"); Document doc; Elements images = new Elements(); try { LOGGER.debug("Loading " + rawUrl);
private Elements siblings(String query, boolean next, boolean all) { Elements els = new Elements(); Evaluator eval = query != null? QueryParser.parse(query) : null; for (Element e : this) { do { Element sib = next ? e.nextElementSibling() : e.previousElementSibling(); if (sib == null) break; if (eval == null) els.add(sib); else if (sib.is(eval)) els.add(sib); e = sib; } while (all); } return els; }
DesignContext context) { List<Component> descriptions = new ArrayList<>(); Elements content = new Elements();
Elements divs = document.select("div"); Elements innerMostDivs = new Elements(); for (Element div : divs) { if (div.select(">div").isEmpty()) { innerMostDivs.add(div); } } // ...
private Scope(Elements context){ super(); this.context = new Elements(); this.context.addAll(context); } private Scope(Element context){
public Elements getElements() { Elements elements = new Elements(); for (XElements xElements : elementsList) { elements.addAll(xElements.getElements()); } return elements; }
@Override public XValue apply(Elements context) { Set<Element> total = new HashSet<>(); Elements parents = new Elements(); for (Element el:context){ total.add(el.parent()); } parents.addAll(total); return XValue.create(parents); } }
@Override public XValue apply(Elements context) { Set<Element> total = new HashSet<>(); Elements ancestor = new Elements(); for (Element el:context){ total.addAll(el.parents()); } ancestor.addAll(total); return XValue.create(ancestor); } }
public static Elements followingSibling(Element el){ Elements rs = new Elements(); Element tmp = el.nextElementSibling(); while (tmp!=null){ rs.add(tmp); tmp = tmp.nextElementSibling(); } if (rs.size() > 0){ return rs; } return null; }
@Override public XValue apply(Elements context) { Elements childs = new Elements(); for (Element el:context){ childs.addAll(el.children()); } return XValue.create(childs); } }
String input = "<div>a</div><p>b</p><strong>c</strong>"; Document doc = Jsoup.parse(input); Elements elements = doc.select("p ~ *"); Elements group = new Elements(); group.add(elements.first().previousElementSibling()); for( Element element : elements ) { group.add(element); } // You can work with 'group' too String output = group.toString();