Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取jsoup中特定标记类型的下一个元素_Java_Jsoup - Fatal编程技术网

Java 获取jsoup中特定标记类型的下一个元素

Java 获取jsoup中特定标记类型的下一个元素,java,jsoup,Java,Jsoup,我正在使用jsoup遍历元素列表,但需要定期查找一个不直接出现在当前元素之后的元素 例如,如果我在遍历一个img标记,我想找到在该img标记之后出现的下一个a标记。但是,两者之间可能有一些标签 下面是一些示例代码: for (Element e : elements) { if (e.tagName().equalsIgnoreCase("img")) { // Do some stuff with "img" tag // Now, find the n

我正在使用jsoup遍历元素列表,但需要定期查找一个不直接出现在当前元素之后的元素

例如,如果我在遍历一个
img
标记,我想找到在该
img
标记之后出现的下一个
a
标记。但是,两者之间可能有一些标签

下面是一些示例代码:

for (Element e : elements) {
    if (e.tagName().equalsIgnoreCase("img")) {
        // Do some stuff with "img" tag
        // Now, find the next tag in the list with tag <a>
    }

    // Do some other things before the next loop iteration
}
for(元素e:元素){
如果(例如标记名().equalsIgnoreCase(“img”)){
//做一些有“img”标签的事情
//现在,使用tag查找列表中的下一个标记
}
//在下一个循环迭代之前做一些其他事情
}
我认为像
e.select(“img~a”)
这样的东西应该可以工作,但它不会返回任何结果

在jsoup中执行此操作的好方法是什么?

使用nextElementSibling()方法

在if语句中添加以下代码:

Element imgNext = e.nextElementSibling();
do {
    Element a = imgNext.select("a[href]").first();         
} while (imgNext!=null && a==null);
这似乎是实现既定目标的一种方式。不确定它是最有效的,但它是最直接的

Element node = e.nextElementSibling();
while (node != null && !node.tagName().equalsIgnoreCase("a")) {
    node = node.nextElementSibling();
}

我希望有一种方法可以运行相当于
e.nextElementSibling(“a”)
。也许我可以为jsoup做些贡献;-)

这实际上是一个无限循环。如果第一次通过时未发现“a”不是
null
以外的内容,则循环将永远不会结束。