2017-04-19 106 views
0

我想使用Jsoup解析<dl>标记。 <dl>标签包含<dt>标签和<dd>标签。我有一个HashMap(HashMap<String, List<String>),其中我想把<dt> S作为键和<dd> S作为值(有每<dt>标签的多个<dd>标签使用Jsoup解析dl标记

的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. 

回答

2

你需要插入复制你的描述列表到地图,你目前操作列表中的1个实例。因此,而不是:

block.fields.put(title, descriptions); 

创建一个新的列表,例如:

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

这工作,谢谢 – bramhaag