/** * Set an attribute value on this element. If this element already has an attribute with the * key, its value is updated; otherwise, a new attribute is added. * * @return this element */ public Element attr(String attributeKey, String attributeValue) { super.attr(attributeKey, attributeValue); return this; }
private boolean isNotNullSpan(Node node) { return !(node.hasAttr("class") && node.attr("class").equals("null")); }
/** * 处理style属性 * * @param tag * @param eleJsonObj */ private void processStyle(Node node, String tag, JSONObject eleJsonObj) { if (node.hasAttr("style")) { String style = node.attr("style"); if (StrKit.notBlank(style)) { JSONObject attr = eleJsonObj.getJSONObject("attr"); if (attr == null) { attr = new JSONObject(); eleJsonObj.put("attr", attr); } attr.put("style", style); } } }
@Override public String attr(String key) { Validate.notNull(key); if (!hasAttributes()) { return key.equals(nodeName()) ? (String) value : EmptyString; } return super.attr(key); }
@Override public Node attr(String key, String value) { if (!hasAttributes() && key.equals(nodeName())) { this.value = value; } else { ensureAttributes(); super.attr(key, value); } return this; }
/** * 处理class属性 * * @param node * @param tag * @param eleJsonObj */ private void processClass(Node node, String tag, JSONObject eleJsonObj) { if (node.hasAttr("class")) { String style = node.attr("class"); if (StrKit.notBlank(style)) { JSONObject attr = eleJsonObj.getJSONObject("class"); if (attr == null) { attr = new JSONObject(); eleJsonObj.put("attr", attr); } attr.put("class", style); } } }
return ""; // nothing to make absolute with } else { return StringUtil.resolve(baseUri(), attr(attributeKey));
/** * Writes the package mappings (prefix -> package name) of this object to * the specified document. * <p> * The prefixes are stored as <meta> tags under <head> in the document. * * @param doc * the Jsoup document tree where the package mappings are written */ public void writePackageMappings(Document doc) { Element head = doc.head(); for (String prefix : getPackagePrefixes()) { // Only store the prefix-name mapping if it is not a default mapping // (such as "vaadin" -> "com.vaadin.ui") if (!VAADIN_PREFIX.equals(prefix) && !VAADIN7_PREFIX.equals(prefix) && !LEGACY_PREFIX.equals(prefix)) { Node newNode = doc.createElement("meta"); newNode.attr("name", "package-mapping"); String prefixToPackageName = prefix + ":" + getPackage(prefix); newNode.attr("content", prefixToPackageName); head.appendChild(newNode); } } }
/** * Private method for writing position attributes * * @since 7.4 * @param node * target node * @param key * attribute key * @param symbol * value symbol * @param value * the value */ private void writePositionAttribute(Node node, String key, String symbol, Float value) { if (value != null) { String valueString = DesignAttributeHandler.getFormatter() .format(value); node.attr(key, valueString + symbol); } }
public void tail(Node node, int depth) { if (depth == 0) resultSet.add(node.attr("href")); }
private static int line(Node node) { return Integer.valueOf(node.attr(LINE_NUMBER_ATTRIBUTE)); }
private void lines(Node node, String data) { Matcher newLinematcher = LINE_SEPARATOR.matcher(data); while (newLinematcher.find()) { linecount++; } node.attr(LINE_NUMBER_ATTRIBUTE, String.valueOf(linecount)); }
private void lines(Node node, String data) { Matcher newLinematcher = LINE_SEPARATOR.matcher(data); while (newLinematcher.find()) { linecount++; } node.attr(LINE_NUMBER_ATTRIBUTE, String.valueOf(linecount)); }
/** * Each HTML or HTML5 document must begin with a valid doctype declaration. * Is considered valid a doctype with "html" name and no attribute. */ public void validateRpd6s1() { boolean validDocumentType = this.html5Document.childNodes().stream() .filter(node -> node instanceof DocumentType) .anyMatch(documentType -> "html".equalsIgnoreCase(documentType.attr("name")) && StringUtils .isAllEmpty(documentType.attr("publicId"), documentType.attr("systemId"))); assertTrue(Type.ERROR, "rpd6s1.doctype", validDocumentType); }
public void parseCollegeYears(String rawHtml, Setting setting) { Element doc = Jsoup.parse(rawHtml).getElementById("xnd"); for (Node yearNode : doc.childNodes()) { if (yearNode.hasAttr("value")) { setting.ownYears.add(yearNode.attr("value")); if (yearNode.hasAttr("selected")) { setting.currentYear = yearNode.attr("selected"); } } } }
String htmlString = "<html><div><p><strong>\"notClickable1\"<a rel=\"nofollow\" target=\"_blank\" href=\"example1.com\">clickable</a>\"notClickable2\"<a rel=\"nofollow\" target=\"_blank\" href=\"example2.com\">clickable</a>\"notClickable3\"<a rel=\"nofollow\" target=\"_blank\" href=\"example3.com\">clickable</a></strong></p></div></html>"; Document doc = Jsoup.parse(htmlString); //can be replaced with Jsoup.connect("yourUrl").get(); String parsedHTML = ""; Element container = doc.select("div>p>strong").first(); for (Node node : container.childNodes()) { if(node.nodeName().equals("a") && node.previousSibling().nodeName().equals("#text")){ parsedHTML += node.previousSibling().toString().replaceAll("\"", ""); parsedHTML += "= " + node.attr("href").toString() + " "; } } parsedHTML.trim(); System.out.println(parsedHTML);