我不清楚<div>
您要转换为哪个标签<tr>
并进行<td>
标记。
但是,我假设 DivClass-1、DivClass-2、DivClass-3、DivClass-4 转换为<tr>
标签。其他的则转换为<td>
标签。
我希望下面的代码能给你一点想法。
StringBuffer myHTML = new StringBuffer();
myHTML.append("<div class=\"DivClass-1\"> Div One <div class=\"DivClass-A\"> Div A </div> </div>" +
"<div class=\"DivClass-2\"> Div Two<div class=\"DivClass-A\"> Div B </div></div>" +
"<div class=\"DivClass-3\"> Div Three<div class=\"DivClass-A\"> Div C </div></div>" +
"<div class=\"DivClass-4\"> Div Four <div class=\"DivClass-A\"> Div D </div></div>");
Document myDoc = Jsoup.parse(myHTML.toString());
//get DivClass-1, DivClass-2, etc.
Elements DivClass = myDoc.select("div").not("div.DivClass-A");
Elements DivClass_A = myDoc.select("div.DivClass-A");
//rename the tag <div class="DivClass-1"> to <tr class="DivClass-1">
DivClass.tagName("tr");
//renamed the tag <div class="DivClass-A"> to <td class="DivClass-A">
DivClass_A.tagName("td");
System.out.println(myDoc.toString());
这是打印输出-
<tr class="DivClass-1">
Div One
<td class="DivClass-A"> Div A </td>
</tr>
<tr class="DivClass-2">
Div Two
<td class="DivClass-A"> Div B </td>
</tr>
<tr class="DivClass-3">
Div Three
<td class="DivClass-A"> Div C </td>
</tr>
<tr class="DivClass-4">
Div Four
<td class="DivClass-A"> Div D </td>
</tr>