【问题标题】:Capturing Id attribute to an Array by JSoup?通过 JSoup 将 Id 属性捕获到数组?
【发布时间】:2013-09-05 16:04:29
【问题描述】:

我正在使用库 Jsoup,是我有一个字符串,其中包含两个 HTML 组件,其属性 ID 为 all,我想要做的就是在一个数组中捕获这两个 ID。

String chain = "<div id='stylized' class='myform' style='margin:20px auto;'>
            <div id='material_comprado'  > </div> ";

我想用这个,但是失败了。

int i = 0;
Elements values = doc.getElementsByAttribute("id");
String s[] = new String[values.size()];
for(Element el : values){
    s[i++] = el.attr("id");
    System.out.println("==> "+s[i]);
}

任何人都可以帮助我。

【问题讨论】:

    标签: java html filter jsoup


    【解决方案1】:

    您的JSoup 代码本身没问题。

    当您尝试显示该元素时,您将s 的数组索引增加到超出其上限,从而导致ArrayIndexOutOfBoundsException你完成对数组的访问之后增加索引

    for (Element el : values){
        s[i] = el.attr("id");
        System.out.println("==> " + s[i]);
        i++; // now safe to increment
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 1970-01-01
      相关资源
      最近更新 更多