I am trying to parse a <dl>
tag using Jsoup. The <dl>
tag contains <dt>
tags and <dd>
tags. 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.
The HTML looks like this:
<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. So instead of:
block.fields.put(title, descriptions);
create a new list, eg:
block.fields.put(title, new ArrayList<>(descriptions));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.