简体   繁体   English

通过JSoup将ID属性捕获到数组吗?

[英]Capturing Id attribute to an Array by JSoup?

I'm using the library Jsoup, is that I have a string with two HTML components with the attribute ID to all, I want to do is capture the two IDs in an array. 我正在使用库Jsoup,是我有一个带有两个HTML组件的字符串,所有HTML组件的属性ID都为ID,我想做的就是捕获数组中的两个ID。

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

I was trying to use this for, but failed. 我试图将其用于,但失败了。

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]);
}

Anyone can help me. 任何人都可以帮助我。

Your JSoup code itself is fine. 您的JSoup代码本身很好。

You're incrementing the array index for s beyond its upper bound resulting in an ArrayIndexOutOfBoundsException when you attempt to display the element. 当您尝试显示元素时,将s的数组索引递增到其上限之外,从而导致ArrayIndexOutOfBoundsException Increment the index after youve finished accessing the array 完成访问数组后,增加索引

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM