【问题标题】:Parsing dl tag using Jsoup使用 Jsoup 解析 dl 标签
【发布时间】:2017-04-19 13:59:38
【问题描述】:

我正在尝试使用 Jsoup 解析 <dl> 标记。 <dl> 标签包含<dt> 标签和<dd> 标签。我有一个 HashMap (HashMap<String, List<String>),我想将 <dt>s 作为键和 <dd>s 作为值(每个 <dt> 标签有多个 <dd> 标签。

HTML 如下所示:

<dl>
<dt>
    <span class="paramLabel">Parameters:</span>
</dt>
<dd>
    <code>y</code> - the ordinate coordinate
</dd>
<dd>
    <code>x</code> - the abscissa coordinate
</dd>
<dt>
    <span class="returnLabel">Returns:</span>
</dt>
<dd>
    the <i>theta</i> component of the point (<i>r</i>,&nbsp;<i>theta</i>) in polar coordinates that corresponds to the point (<i>x</i>,&nbsp;<i>y</i>) in Cartesian coordinates.
</dd>

我尝试了以下方法:

String title = "";
List<String> descriptions = new ArrayList<>();
for (int i = 0; i < children.size(); i++) {
    Element child = children.get(i);

    if(child.tagName().equalsIgnoreCase("dt")) {
        if(descriptions.size() != 0) {
            block.fields.put(title, descriptions);
            descriptions.clear();

        }

        title = child.text();
    }
    else if(child.tagName().equalsIgnoreCase("dd")) {
        descriptions.add(child.text());

        if(i == children.size() - 1) {
            block.fields.put(title, descriptions);
        }
    }
}

我希望得到这个:

 * Parameters -> y - the ordinate coordinate
 *               x - the abscissa coordinate
 * Returns    -> the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.

但我得到了这个:

 * Parameters -> the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.


 * Returns    -> the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.

【问题讨论】:

    标签: java html jsoup


    【解决方案1】:

    您需要将描述列表的副本插入到地图中,目前您操作列表的 1 个实例。所以而不是:

    block.fields.put(title, descriptions);
    

    创建一个新列表,例如:

    block.fields.put(title, new ArrayList<>(descriptions));
    

    【讨论】:

      猜你喜欢
      • 2014-04-25
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 2016-06-10
      相关资源
      最近更新 更多