VictorCreator
VictorCreator

Reputation: 744

How to get related classes and values in JSoup?

I have an HTML file, a part of which looks like this:

<a name="user_createtime"></a>
<p class="column">
<span class="coltitle">CreateTime</span>&nbsp;&nbsp;<span class="titleDesc"><span class='defPopupLink' onClick='popupDefinition(event, "datetime")'>datetime</span></span>&nbsp;&nbsp;&nbsp;&nbsp;<span class = "spaceandsize">(non-null)<sup><span class='glossaryLink' onclick="popupDefinition(event, '<b>non-null</b><br>The column cannot contain null values.')">?</span></sup></span>
<br>
<span class="desc">Timestamp when the object was created</span>

<a name="user_createuser"></a>
<p class="column">
<span class="coltitle">CreateUser</span>&nbsp;&nbsp;<span class="titleDesc">foreign key to <A HREF="User.html" TARGET="tableFrame">User</A></span>&nbsp;&nbsp;&nbsp;&nbsp;
<span class = "spaceandsize">(database column: CreateUserID)</span>
<br>
<span class="desc">User who created the object</span>

There are many such Coltitle. titleDesc and desc classes.

Now, if I get an input string like "CreateTime", I want the output to be:

CreateTime, datetime, Timestamp when the object was created 

and if I get an input string "CreateUser", I want the output to be:

CreateUser,  foreign key to User, User who created the object 

I'm using Jsoup for this, and I have gotten this far:

Elements colElements = Jsoup.parse(html).getElementsByClass("coltitle").select("*");


System.out.println("your Col:");
for (Element element : colElements)
{
    if(element.ownText().equalsIgnoreCase("CreateTime"))
        System.out.println(element.text());
}

which just prints the selected coltitle. How do I parse the related classes and get their values? Or, are they not even related and am I just treading down the wrong path? Can someone please help me get my desired output?

Upvotes: 1

Views: 301

Answers (2)

Daniel B
Daniel B

Reputation: 8879

You are only selecting the <span>-tags, thus, only printing what they values they hold.

You can use the siblingElements()-method to get the siblings of the element that you first select.

Your HTML does not seem to be formatted correctly, but the following should work

System.out.println("your Col:");
for (Element element : colElements) {
    if (element.ownText().equalsIgnoreCase("CreateTime")) {
        System.out.print(element.text());
        for (Element sibling : element.siblingElements()) {
            System.out.print(", " + sibling.text());
        }
    }
    if (element.ownText().equalsIgnoreCase("CreateUser")) {
        System.out.print("\n"+element.text());
        for (Element sibling : element.siblingElements()) {
            System.out.print(", " + sibling.text());
        }
    }
}

This will select the elements of the class 'colTitle'. The if-case will check if it's either of them, and then print out the element text. It will then move on to it's siblings, and print out their texts.

Upvotes: 1

Rob Garwood
Rob Garwood

Reputation: 108

According to the api docs, you can call children() on colElements.

http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#children()

Upvotes: 0

Related Questions