1

I have a html table & want to extract link text based on certain condition

<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tbody>
<tr class="dir"><td colspan="2">&nbsp;&nbsp;<a href="http://xyz/">Yogendra sharma</a></td></tr>
<tr>
<td class="f"><a href="abc">abc</a>&nbsp;</td>
<td>
<tt class="con">
<a class="s" href="mno"><span class="l">7</span> mno <b>Hello</b>;</a>
<br>
</tt>
</td></tr>

<tr class="dir"><td colspan="2">&nbsp;&nbsp;<a href="http://xyz/">Yogendra</a></td></tr>
<tr>
<td class="f"><a href="abc">abc</a>&nbsp;</td>
<td>
<tt class="con">
<a class="s" href="mno"><span class="l">7</span> mno <b>Hello</b>;</a>
<br>
</tt>
</td></tr>
</table>

i want to print all first link text i.e Yogendra Sharma & Yogendra for html file.

this file is huge.

i use java with jsoup but cant figger it out. please help me .

0

1 Answer 1

2

You can try the below code. You would need commons-io-1.3.2.jar , jsoup.jar. Save the html as sample.html in the root folder of project.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ExtractFromHTML {

    public static void main(String[] args) throws IOException {

        File input = new File("sample.html");

        InputStream in = new FileInputStream(input);

        String htmlOut = IOUtils.toString(in);

        Document document = Jsoup.parse(htmlOut);

        Elements elementsA = document.select("a");

        Iterator<Element> elementIterator = elementsA.iterator();

        while (elementIterator.hasNext()) {
            Element aElement = elementIterator.next();

            if (aElement.outerHtml().contains("http://xyz/")) {
                System.out.println(aElement.text());
            }

        }
    }
}

Output :

Yogendra sharma
Yogendra
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.