Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.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_Html_Jsoup - Fatal编程技术网

Java 选择并迭代具有相同名称的元素和子元素(Jsoup)

Java 选择并迭代具有相同名称的元素和子元素(Jsoup),java,html,jsoup,Java,Html,Jsoup,我需要通过jsoup解析一个页面。该页面具有标记为div、h3、a等的元素。我想对这些元素进行解析,然后选择a(即标题)以显示在jList中 例如,该页面如下所示: <div class="start"> <div class="g"> <div class="abc"> <a class="picture" href="www.img.com"><img src="img" alt="imag

我需要通过jsoup解析一个页面。该页面具有标记为
div
h3
a
等的元素。我想对这些元素进行解析,然后选择
a
(即标题)以显示在
jList

例如,该页面如下所示:

<div class="start">
    <div class="g">
        <div class="abc">
            <a class="picture" href="www.img.com"><img src="img" alt="image1"></a>
            <div class="xyz">
                <h3 class="_r">
                    <a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">THIS IS <em>example</em>1</a>
                </h3>
            </div>
        </div>
    </div>

    <div class="g">
        <div class="abc">
            <a class="picture" href="www.img.com"><img src="img" alt="image2"></a>
            <div class="xyz">
                <h3 class="_r">
                    <a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">lead by this<em>example</em></a>
                </h3>
            </div>
        </div>
    </div>

    <div class="g">
        <div class="abc">
            <a class="picture" href="www.img.com"><img src="img" alt="image3"></a>
            <div class="xyz">
                <h3 class="_r">
                    <a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">showed<em>example</em>for the people</a>
                </h3>
            </div>
        </div>
    </div>

    <div class="g">
        <div class="abc">
            <a class="picture" href="www.img.com"><img src="img" alt="image4"></a>
            <div class="xyz">
                <h3 class="_r">
                    <a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">we set<em>example</em>for people</a>
                </h3>
            </div>
        </div>
    </div>
</div>

当前,您将刮取的数据分配给循环中的
title
,然后分配给jlist的循环之外的
title
。因此,循环完成后,
title
的值始终是最后一个值

替换这个

for (Element e1 : e) {
    title = e1.getElementsByTag("a").text();
}
DefaultListModel<String> listModel = new DefaultListModel<>();
listModel.addElement(title);
(元素e1:e)的
{
title=e1.getElementsByTag(“a”).text();
}
DefaultListModel listModel=新的DefaultListModel();
listModel.addElement(标题);
用这个

DefaultListModel<String> listModel = new DefaultListModel<>();
for (Element e1 : e) {
    listModel.addElement(e1.getElementsByTag("a").text());
}
DefaultListModel listModel=新的DefaultListModel();
用于(元素e1:e){
addElement(e1.getElementsByTag(“a”).text());
}

当前,您将刮取的数据分配给循环中的
title
,然后分配给jlist的循环之外的
title
。因此,循环完成后,
title
的值始终是最后一个值

替换这个

for (Element e1 : e) {
    title = e1.getElementsByTag("a").text();
}
DefaultListModel<String> listModel = new DefaultListModel<>();
listModel.addElement(title);
(元素e1:e)的
{
title=e1.getElementsByTag(“a”).text();
}
DefaultListModel listModel=新的DefaultListModel();
listModel.addElement(标题);
用这个

DefaultListModel<String> listModel = new DefaultListModel<>();
for (Element e1 : e) {
    listModel.addElement(e1.getElementsByTag("a").text());
}
DefaultListModel listModel=新的DefaultListModel();
用于(元素e1:e){
addElement(e1.getElementsByTag(“a”).text());
}

实际上,您不会每次都添加标题。循环使用找到的新值替换每次标题,并在循环后将其添加到列表中。像这样的东西可能会按照你想要的方式工作:

    DefaultListModel<String> listModel = new DefaultListModel<>();  
    for (Element e1 : e) {
       listModel.addElement(e1.getElementsByTag("a").text());
    }
DefaultListModel listModel=新的DefaultListModel();
用于(元素e1:e){
addElement(e1.getElementsByTag(“a”).text());
}

实际上,您不会每次都添加标题。循环使用找到的新值替换每次标题,并在循环后将其添加到列表中。像这样的东西可能会按照你想要的方式工作:

    DefaultListModel<String> listModel = new DefaultListModel<>();  
    for (Element e1 : e) {
       listModel.addElement(e1.getElementsByTag("a").text());
    }
DefaultListModel listModel=新的DefaultListModel();
用于(元素e1:e){
addElement(e1.getElementsByTag(“a”).text());
}

工作正常!:)。还有一个问题,我怎样才能获得标题的链接?根据,
e1.getElementsByTag(“a”).attr(“href”)
应该可以做到这一点。@user7146946
e1.getElementsByTag(“a”).attr(“href”)
工作得很好!:)。还有一个问题,我怎样才能得到标题的链接呢?根据,
e1.getElementsByTag(“a”).attr(“href”)
应该可以做到这一点。@user7146946
e1.getElementsByTag(“a”).attr(“href”)