【问题标题】:DOM help - jsoup nextSiblingDOM 帮助 - jsoup nextSibling
【发布时间】:2012-03-01 03:11:41
【问题描述】:

我想做的事情对我来说似乎很简单,但我的努力远远超出了我应该做的。我有一份文件,其中包含以下内容:

<h2>First Heading</h2>
<table>
    <div class="title">First Subheading One</div>
    <div class="title">First Subheading Two</div>
    <div class="title">First Subheading Three</div>
</table>

<h2>Second Heading</h2>
<table>
    <div class="title">Second Subheading One</div>
    <div class="title">Second Subheading Two</div>
    <div class="title">Second Subheading Three</div>
</table>

<h2>Third Heading</h2>
<table>
    <div class="title">Third Subheading One</div>
    <div class="title">Third Subheading Two</div>
    <div class="title">Third Subheading Three</div>
</table>

使用 doc.select("h2") 给了我所有的标题,正如预期的那样。使用 doc.select("div.title") 给了我所有的副标题,也如预期的那样。我要做的是遍历返回的 h2 元素,然后在其中遍历返回的 div.title 元素-我尝试了很多东西,而且我对编码一点也不陌生(jsoup 的新手,但是)但我似乎无法理解如何做到这一点。

Headings = httpDoc.select("h3");
for(Element Headings : heading) {
    // something with heading.nextSibling here
}

应该有什么我可以做的(例如nextSibling)给我节点吗?从那里我可以做另一个 select("div.title") 并遍历那些以获取子标题?

或者我是完全错误的方式吗?如果这看起来很愚蠢,我深表歉意——感觉有点愚蠢,因为我编码的时间比我愿意承认的要长,但从来没有处理过 DOM(一直是 Win32 人。)

【问题讨论】:

    标签: jsoup


    【解决方案1】:

    我的理解!!

    我从您的问题中了解到的是,您正在尝试获取 h2 标记,然后对于每个 heading &lt;h2&gt;,您正在尝试在表格中获取相应的 div.title

    你的错误

    1. 在提供的代码 sn-p 中,您尝试获取 h3 而不是 h2,您的 HTML 代码中没有。
    2. 其次,HTML sn-p 是一个错误 因为 根据 W3 标准,&lt;table&gt; 应该有 &lt;tr&gt;&lt;td&gt;(我认为 &lt;td&gt; 是可选的,请检查 W3 页面)。因此,当您解析 HTML sn-p 时,jSoup 只是 prunes/removes 格式不正确的 &lt;table&gt;

    根据我对您的问题的理解的预期输出!!

    The header is: First Heading
    The div content is: First Subheading One
    The div content is: First Subheading Two
    The div content is: First Subheading Three
    ========== +_+ ===========
    The header is: Second Heading
    The div content is: Second Subheading One
    The div content is: Second Subheading Two
    The div content is: Second Subheading Three
    ========== +_+ ===========
    The header is: Third Heading
    The div content is: Third Subheading One
    The div content is: Third Subheading Two
    The div content is: Third Subheading Three
    ========== +_+ ===========
    

    上述输出的代码!!

    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    public class JSoupTest 
    {
        public static void main(String[] args) 
        {
            String s = "<h2>First Heading</h2>";
            s += "<table><tr><td>";
            s += "<div class='title'>First Subheading One</div>";
            s += "<div class='title'>First Subheading Two</div>";
            s += "<div class='title'>First Subheading Three</div>";
            s += "</table>";
    
            s += "<h2>Second Heading</h2>";
            s += "<table><tr><td>";
            s += "<div class='title'>Second Subheading One</div>";
            s += "<div class='title'>Second Subheading Two</div>";
            s += "<div class='title'>Second Subheading Three</div>";
            s += "</td></tr></table>";
    
            s += "<h2>Third Heading</h2>";
            s += "<table><tr><td>";
            s += "<div class='title'>Third Subheading One</div>";
            s += "<div class='title'>Third Subheading Two</div>";
            s += "<div class='title'>Third Subheading Three</div>";
            s += "</td></tr></table>";
    
            Document doc = Jsoup.parse(s);
    
            Elements h_2 = doc.select("h2");
            for(int i=0; i<h_2.size(); i++)
            {
                Element e = h_2.get(i);
                System.out.println("The header is: " + e.ownText());
    
                Element nextSib = e.nextElementSibling();
    
                Elements divs = nextSib.select("div.title");            
                for(int j=0; j<divs.size(); j++)
                {
                    Element d = divs.get(j);
                    System.out.println("The div content is: " + d.ownText());
                }
    
                System.out.println("========== +_+ ===========");
            }
        }
    }
    

    【讨论】:

    • 太好了,非常感谢!我的问题是我试图获取错误元素的兄弟。你的例子让我意识到我的错误,我的代码现在按预期工作。谢谢!
    猜你喜欢
    • 2011-09-02
    • 2011-10-21
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 2016-08-07
    • 2021-02-24
    相关资源
    最近更新 更多