使用Jsoup,Android替换元素

时间:2013-08-06 13:15:14

标签: android jsoup

有没有人知道如何使用Jsoup替换元素。我试图用按钮替换表元素及其内容,但没有成功。代码尝试如下。这是一个Android项目

Elements elements = doc.select("table");
         if (elements != null) {
            for (Element element : elements) {
                Element button = Jsoup.parse("<button type='button'>Click Me!</button>");
                element.replaceWith(button);
            }
         }  

1 个答案:

答案 0 :(得分:0)

我以一种似乎工作的hacky方式解决了这个问题。 replaceWith(button)属性没有做任何事情。我确实想用一个按钮替换整个表,但我也想将该按钮与结果一起添加到字符串中。

for (int i = 0; i < elements.size(); i++) {
    Element sibling = siblings.get(i);
    if ("table".equals(sibling.tagName())) {
        siblings.remove(i);
        Element button = Jsoup.parse("<button type='button'>Click Me!</button>");
        sibling = button;
        sb.append(sibling.toString());
     } 
    else {
        sb.append(sibling.toString());
    }
}