这是我要解决的问题。我有一个简单的 HTML 页面:
<html>
<head></head>
<body>
<table>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
<tr>
<td>Goodby</td>
<td>World</td>
</tr>
</table>
</body>
我想要做的是遍历整个树并存储每个文本节点的长度。它不仅应该包含当前节点的长度,而且实际上应该包含所有先前文本节点的长度。让我澄清一下这个例子的意思:
<html>
<head></head>
<body>
<table>
<tr>
<td>Hello</td> // console output should be string of length: 5
<td>World</td> // console output should be string of length: 10
</tr>
<tr>
<td>Goodby</td> // console output should be string of length: 16
<td>World</td> // console output should be string of length: 21
</tr>
</table>
</body>
为此,我实现了以下代码:
private static void print(Node aNode, int aCounter, String aIndent)
{
if(aNode.getNodeValue() != null)
System.out.println(aIndent+aNode.getNodeName() + ", "+aNode.getNodeValue() + ", length: " + aCounter);
else
System.out.println(aIndent+aNode.getNodeName());
Node child = aNode.getFirstChild();
while (child != null)
{
if(child.getNodeValue() != null)
{
aCounter += child.getNodeValue().length();
print(child, aCounter, aIndent+" ");
}
else
print(child, aCounter, aIndent+" ");
child = child.getNextSibling();
}
}
我将根节点传递给此方法。这段代码的问题是它只返回路径的长度。这意味着我得到这样的东西:
<html>
<head></head>
<body>
<table>
<tr>
<td>Hello</td> // console output is string of length: 5
<td>World</td> // console output is string of length: 10
</tr>
<tr>
<td>Goodby</td> // console output should be string of length: 6 <-- does not consider overall length of previous <tr> content
<td>World</td> // console output should be string of length: 11
</tr>
</table>
</body>
所以基本上我想要从根节点到当前标签末尾的所有字符的长度。不幸的是,我不知道该怎么做。任何帮助将不胜感激。先感谢您。