多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 使用DOM方法来遍历一个文档 <div><h2>问题</h2> <p>你有一个HTML文档要从中提取数据,并了解这个HTML文档的结构。</p> <h2>方法</h2> <p>将HTML解析成一个<code><a title="A HTML Document." href="http://jsoup.org/apidocs/org/jsoup/nodes/Document.html">Document</a></code>之后,就可以使用类似于<abbr title="Document Object Model">DOM的方法</abbr>进行操作。示例代码:</p> <pre><code>File input = new File("/tmp/input.html"); Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/"); Element content = doc.getElementById("content"); Elements links = content.getElementsByTag("a"); for (Element link : links) { String linkHref = link.attr("href"); String linkText = link.text(); } </code></pre> <h2>说明</h2> <p>Elements这个对象提供了一系列类似于DOM的方法来查找元素,抽取并处理其中的数据。具体如下:</p> <h3>查找元素</h3> <ul> <li><code><a title="Find an element by ID, including or under this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#getElementById%28java.lang.String%29">getElementById(String id)</a></code></li> <li><code><a title="Finds elements, including and recursively under this element, with the specified tag name." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#getElementsByTag%28java.lang.String%29">getElementsByTag(String tag)</a></code></li> <li><code><a title="Find elements that have this class, including or under this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#getElementsByClass%28java.lang.String%29">getElementsByClass(String className)</a></code></li> <li><code><a title="Find elements that have a named attribute set." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#getElementsByAttribute%28java.lang.String%29">getElementsByAttribute(String key)</a></code> (and related methods)</li> <li>Element siblings: <code><a title="Get sibling elements." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#siblingElements%28%29">siblingElements()</a></code>, <code><a title="Gets the first element sibling of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#firstElementSibling%28%29">firstElementSibling()</a></code>, <code><a title="Gets the last element sibling of this element" href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#lastElementSibling%28%29">lastElementSibling()</a></code>; <code><a title="Gets the next sibling element of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#nextElementSibling%28%29">nextElementSibling()</a></code>, <code><a title="Gets the previous element sibling of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#previousElementSibling%28%29">previousElementSibling()</a></code></li> <li>Graph: <code><a title="Gets this node's parent node." href="http://jsoup.org/apidocs/org/jsoup/nodes/Node.html#parent%28%29">parent()</a></code>, <code><a title="Get this element's child elements." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#children%28%29">children()</a></code>, <code><a title="Get a child element of this element, by its 0-based index number." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#child%28int%29">child(int index)</a></code></li></ul> <h3>元素数据</h3> <ul> <li><code><a title="Get an attribute value from the first matched element that has the attribute." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#attr%28java.lang.String%29">attr(String key)</a></code>获取属性<code><a title="Set an attribute on all matched elements." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#attr%28java.lang.String,%20java.lang.String%29">attr(String key, String value)</a></code>设置属性</li> <li><code><a href="http://jsoup.org/apidocs/org/jsoup/nodes/TextNode.html#attributes%28%29">attributes()</a></code>获取所有属性</li> <li><code><a title="Get the id attribute of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#id%28%29">id()</a></code>, <code><a title="Gets the literal value of this element's &quot;class&quot; attribute, which may include multiple class names, space separated." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#className%28%29">className()</a></code> and <code><a title="Get all of the element's class names." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#classNames%28%29">classNames()</a></code></li> <li><code><a title="Get the combined text of all the matched elements." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#text%28%29">text()</a></code>获取文本内容<code><a title="Set the text content of this text node." href="http://jsoup.org/apidocs/org/jsoup/nodes/TextNode.html#text%28java.lang.String%29">text(String value)</a></code> 设置文本内容</li> <li><code><a title="Get the combined inner HTML of all matched elements." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#html%28%29">html()</a></code>获取元素内HTML<code><a title="Set the inner HTML of each matched element." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#html%28java.lang.String%29">html(String value)</a></code>设置元素内的HTML内容</li> <li><code><a title="Get the combined outer HTML of all matched elements." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#outerHtml%28%29">outerHtml()</a></code>获取元素外HTML内容</li> <li><code><a title="Get the combined data of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#data%28%29">data()</a></code>获取数据内容(例如:script和style标签)</li> <li><code><a title="Get the Tag for this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#tag%28%29">tag()</a></code> and <code><a title="Get the name of the tag for this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#tagName%28%29">tagName()</a></code></li></ul> <h3>操作HTML和文本</h3> <ul> <li><code><a title="Add the supplied HTML to the end of each matched element's inner HTML." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#append%28java.lang.String%29">append(String html)</a></code>, <code><a title="Add the supplied HTML to the start of each matched element's inner HTML." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#prepend%28java.lang.String%29">prepend(String html)</a></code></li> <li><code><a title="Create and append a new TextNode to this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#appendText%28java.lang.String%29">appendText(String text)</a></code>, <code><a title="Create and prepend a new TextNode to this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#prependText%28java.lang.String%29">prependText(String text)</a></code></li> <li><code><a title="Create a new element by tag name, and add it as the last child." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#appendElement%28java.lang.String%29">appendElement(String tagName)</a></code>, <code><a title="Create a new element by tag name, and add it as the first child." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#prependElement%28java.lang.String%29">prependElement(String tagName)</a></code></li> <li><code><a title="Set the inner HTML of each matched element." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html#html%28java.lang.String%29">html(String value)</a></code></li></ul></div>