JAVA解析HTML,獲取待定元素屬性

Document doc = Jsoup.parseBodyFragment(previewHtml); //html內容解析爲Document
int index = 0;
StringBuffer stringBuffer = new StringBuffer();
Elements inputArray = doc.getElementsByTag("input");//對應的元素數組
for(int a=0;a<inputArray.size(); a++) {
Element element = inputArray.get(a);
String type = element.attr("type"); //獲取元素屬性的值
String field = element.attr("name");
if("text".equals(type) && index<3) {
String title = element.attr("title");
stringBuffer.append("{field:'"+field+"',title:'"+title+"',width:100,align:'center'},");
index++;
} else break;
}
System.out.println(stringBuffer);html

相關文章
相關標籤/搜索