[英]Parsing dl tag using Jsoup
I am trying to parse a <dl>
tag using Jsoup. 我正在尝试使用Jsoup解析
<dl>
标记。 The <dl>
tag contains <dt>
tags and <dd>
tags. <dl>
标记包含<dt>
标记和<dd>
标记。 I have a HashMap ( HashMap<String, List<String>
) in which I want to put the <dt>
s as keys and <dd>
s as values (there are multiple <dd>
tags per <dt>
tag. 我有一个HashMap(
HashMap<String, List<String>
),其中我想将<dt>
用作键,将<dd>
用作值(每个<dt>
标记有多个<dd>
<dt>
标记。
The HTML looks like this: 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>, <i>theta</i>) in polar coordinates that corresponds to the point (<i>x</i>, <i>y</i>) in Cartesian coordinates.
</dd>
I've tried the following: 我尝试了以下方法:
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);
}
}
}
I expected to get this: 我希望得到这个:
* 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.
But I got this: 但是我得到了这个:
* 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.
You need to insert a copy of your descriptions list into the map, currently you manipulate 1 instance of the list. 您需要在地图中插入描述列表的副本 ,当前您正在操作列表的1个实例。 So instead of:
所以代替:
block.fields.put(title, descriptions);
create a new list, eg: 创建一个新列表,例如:
block.fields.put(title, new ArrayList<>(descriptions));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.