在Jsoup中替换元素获取IllegalArgumentException时?

时间:2017-07-27 12:46:36

标签: jsoup

我正在尝试替换具有colspan属性的td,获取attr值我将相应地替换td。

这是我的代码

public static String getParsedHtml(String html)throws Exception{
        try {
            if(html!=null && !html.trim().isEmpty()){
                Document htmlDoc=Jsoup.parse(html);
                if(htmlDoc!=null){
                    System.out.println(" >>>>> " +htmlDoc );
                    Elements colspanElms=htmlDoc.select("td[colspan]");
                    if(colspanElms!=null && colspanElms.size() > 0){
                        for(int col=0;col<colspanElms.size();col++){
                            Element colspanElmt=colspanElms.get(col);
                            String tdValue=colspanElmt.text();
                            int colspanValue=Integer.parseInt(colspanElmt.attr("colspan"));
                            for(int tdGen=0;tdGen<colspanValue;tdGen++){
                                String template="<td>"+tdValue+"</td>";
                                Element replaceElm = Jsoup.parse(template, "", Parser.xmlParser());
                                colspanElmt.replaceWith(replaceElm);
                            }
                        }
                    }else{
                        System.out.println("colspanElms is null or empty"+colspanElms);
                    }
                }
                html=htmlDoc.html();
            }else{
                System.out.println("html is null or empty");
            }
        } catch (Exception e) {
            throw e;
        }
        return html;
    }

我通过调试检查,第二次循环

for(int tdGen=0;tdGen<colspanValue;tdGen++){
    String template="<td>"+tdValue+"</td>";
    Element replaceElm = Jsoup.parse(template, "", Parser.xmlParser());
    colspanElmt.replaceWith(replaceElm);
    }

更新

<td class="td_0_1" colspan="3">Seguros de Personas</td>

我有上面的TD与colspan值,我需要用colspan值的数量替换TD。例如,我需要用3 TD的

代替TD

我收到 IllegalArgumentException:Object为null ,但该对象有值。

帮助表示赞赏。感谢

2 个答案:

答案 0 :(得分:0)

我这里没有编译器来测试,但你确定循环正在做你想要的吗?因为你在循环中替换colspanElmt,但它只在循环外设置一次!所以循环将不断替换相同的元素。

这让我猜测,Element replaceElm并没有真正正确构建,实际上是null。您可以使用调试器或println进行检查。这符合您的发现,因为您第一次在循环中将colspanElmt替换为null,这有效,但是第二次尝试在null上调用replaceWith时,这会给您一个异常

答案 1 :(得分:0)

我找到了自己的答案..

这是我的代码

public static String getParsedHtml(String html)throws Exception{
        try {
            if(html!=null && !html.trim().isEmpty()){
                Element htmlDoc=Jsoup.parse(html, "", Parser.xmlParser());
                if(htmlDoc!=null){
                    Elements tdElements=htmlDoc.select("tr > td[colspan]");
                    if(tdElements!=null && tdElements.size() > 0){
                        int tdSize=tdElements.size();
                        String tdTemp="";
                        String temp="";
                        for(int td=0;td<tdSize;td++){
                            Element colSpanElement=tdElements.get(td);
                            temp=colSpanElement.toString();
                            String attrValue=colSpanElement.attr("colspan");
                            if(attrValue!=null && !attrValue.trim().isEmpty()){
                                int iColSpanVal=Integer.parseInt(attrValue);
                                for(int col=0;col<iColSpanVal;col++){
                                    Element newTd=tdElements.get(td).clone();
                                    newTd.removeAttr("colspan");
                                    tdTemp=tdTemp+" "+newTd.toString();
                                }
                                System.out.println("tdTemp: " + tdTemp);
                            }
                            if(!temp.trim().isEmpty() && !tdTemp.trim().isEmpty()){
                                System.out.println("temp: " + temp);
                                System.out.println("tdTemp: " + tdTemp);
                                html=html.replace(temp, tdTemp);
                                temp="";
                                tdTemp="";
                            }
                        }//End of Td loop
                    }else{
                        System.out.println("tdElements is null or empty : "+tdElements);
                    }

                }else{
                    System.out.println("htmlDoc is null "+htmlDoc);
                }
            }else{
                System.out.println("html is null or empty " + html);
            }
        } catch (Exception e) {
            throw e;
        }
        System.out.println("html:: " + html);
        return html;
    }

感谢您的帮助。 :)

相关问题