<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 使用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>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看