2012-07-24 59 views
1

我必須解析html頁面。我必須在下面的HTML中提取名稱元素的值,該值被分配給一個javascript函數。我如何使用JSoup來做到這一點。如何在解析html頁面時從html頁面的javascript函數中提取變量的值

<input type="hidden" name="fields.DEPTID.value"/> 

JS:

departmentId.onChange = function(value) {          
    var departmentId = dijit.byId("departmentId"); 

    if (value == null || value == "") { 
     document.transferForm.elements["fields.DEPTID.value"].value = ""; 
     document.transferForm.elements["fields.DEPTID_DESC.value"].value = ""; 
    } else { 
     document.transferForm.elements["fields.DEPTID.value"].value = value; 
     document.transferForm.elements["fields.DEPTID_DESC.value"].value = departmentId.getDisplayedValue(); 

     var locationID = departmentId.store.getValue(departmentId.item, "loctID"); 
     var locationDesc = departmentId.store.getValue(departmentId.item, "loct"); 

     locationComboBox = dijit.byId("locationId"); 

     if (locationComboBox != null) { 
      if (locationID != "") { 
       setLocationComboBox(locationID, locationDesc); 
      } else { 
       setLocationComboBox("AMFL", "AMFL - AMY FLORIDA"); 
      } 
     } 
    } 
}; 

回答

0

我會盡力教你形成頂部:

//Connect to the url, and get its source html 
Document doc = Jsoup.connect("url").get(); 

//Get ALL the elements in the page that meet the query 
//you passed as parameter. 
//I'm querying for all the script tags that have the 
//name attribute inside it 
Elements elems = doc.select("script[name]"); 

//That Elements variable is a collection of 
//Element. So now, you'll loop through it, and 
//get all the stuff you're looking for 
for (Element elem : elems) { 
    String name = elem.attr("name"); 

    //Now you have the name attribute 
    //Use it to whatever you need. 
} 

現在如果你想一些幫助Jsoup querys得到任何其他的元素,你可能需要,在這裏你去API文檔來幫助:Jsoup selector API

希望幫助=)

+0

我需要腳本中的屬性「name」值。但是,我不會寫腳本的名字,因爲我必須編寫一個通用解析器來解析任何html頁面。我不知道如何使用上面的代碼,因爲我不知道如何替換腳本。你能否請澄清上述代碼。 – user1445177 2012-07-25 18:49:33