2014-12-25 129 views
-1

這裏是一個html代碼。我想打印「顏色:」和各種顏色選項。不知何故,我想通過使用「選擇名稱= att1」這意味着通過名稱標籤的選擇。JSOUP從<select name = ...>提取數據。

<div class="box-body"> 

    <div id="attributeInputs" class="attribute-inputs" data-defcolor="Palm"> 

     <div class="row thinpad-top att1row"> 
      <div class="small-24 columns"> 
       <label for="att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2">Color:</label> 
      </div> 
      <div class="small-24 columns"> 
       <select name="att1" id="att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2"> 
        <option value="">Please Select Color</option> 
        <option value="Black">Black</option> 
        <option value="Palm">Palm</option> 
       </select> 
      </div> 

我試過這麼多jsoup標籤。但我沒能獲得所需的輸出

我想輸出是這樣的:

Please Select Color: 
Black 

Palm 

請幫助

回答

0

此代碼將提取選擇標籤和選項標籤內的元素

String html="<div class=\"box-body\">\n" + 
         "\n" + 
         " <div id=\"attributeInputs\" class=\"attribute-inputs\" data-defcolor=\"Palm\">\n" + 
         "\n" + 
         "  <div class=\"row thinpad-top att1row\">\n" + 
         "   <div class=\"small-24 columns\">\n" + 
         "    <label for=\"att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2\">Color:</label>\n" + 
         "   </div>\n" + 
         "   <div class=\"small-24 columns\">\n" + 
         "    <select name=\"att1\" id=\"att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2\">\n" + 
         "     <option value=\"\">Please Select Color</option>\n" + 
         "     <option value=\"Black\">Black</option>\n" + 
         "     <option value=\"Palm\">Palm</option>\n" + 
         "    </select>\n" + 
         "   </div>"; 

       Document doc = Jsoup.parse(html); 
       Elements links = doc.select("select option"); 
       for (Element link : links) { 
        String linkText = link.text(); 
        System.out.println(linkText); 
       } 
+1

但是可以有更多的選項標籤。因此,最好在選擇標籤中只嵌套選項標籤 –

相關問題