Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4239216
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:00:20+00:00 2026-05-21T03:00:20+00:00

using htmlparser (http://htmlparser.sourceforge.net/) I have been trying to extract information (Content1 + Link) from

  • 0

using htmlparser (http://htmlparser.sourceforge.net/) I have been trying to extract information (Content1 + Link) from a html table.

sample html:

<td class="xx">
    <a href="http://link">Content1</a>
</td>

java code:

CssSelectorNodeFilter cssFilter = new CssSelectorNodeFilter("td[class=\"xx\"]");
NodeList nodes = parser.parse(cssFilter);

resultSet = new String[nodes.size()][2];

for (int i=0;i<nodes.size();i++) {
    resultSet[i][0]=nodes.elementAt(i).toPlainTextString().trim();

    LinkTag tag = (LinkTag) (nodes.elementAt(i));
    resultSet[i][1]=tag.getLink();
}

I can extract the first part (the Content1 String) with no problems, but I am having trouble getting the link. It either says I cannot cast on a TextNode (with the code above) or it returns null.

as above – result: TableColumn cannot be cast to LinkTag

LinkTag tag = (LinkTag) (nodes.elementAt(i));
resultSet[i][1]=tag.getLink();

result: TextNode cannot be cast to LinkTag

   LinkTag tag = (LinkTag) (nodes.elementAt(i).getFirstChild());
    resultSet[i][1]=tag.getLink();

result: NullPointer

 LinkTag tag = (LinkTag) (nodes.elementAt(i).getFirstChild().getFirstChild());
    resultSet[i][1]=tag.getLink();

result: returns null

 Tag tag = (Tag) (nodes.elementAt(i));
    resultSet[i][1]=tag.getAttribute("href");

Thanks for any ideas/solutions =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:00:21+00:00Added an answer on May 21, 2026 at 3:00 am

    If you print out the contents of the <TD> tag, you get:

    Tag (27[2,8],42[2,23]): td class="xx"
      Txt (42[2,23],56[3,12]): \n
      Tag (56[3,12],75[3,31]): a href="foo.html"
        Txt (75[3,31],78[3,34]): bar
        End (78[3,34],82[3,38]): /a
      Txt (82[3,38],92[4,8]): \n
      End (92[4,8],97[4,13]): /td
    

    Therefore what you want is the sibling of the first child of the TD – though you are then at the mercy of whatever formatting is in the table.

    To find the first link in the table data, you can use this code:

    public static void main(String[] args) throws Exception {
        Parser parser = new Parser("file:test.html");
        CssSelectorNodeFilter cssFilter = new CssSelectorNodeFilter("td[class=\"xx\"]");
        NodeList nodes = parser.parse(cssFilter);
        String[][] resultSet = new String[nodes.size()][2];
        for (int i=0;i<nodes.size();i++) {
            Node n = nodes.elementAt(i);
            System.out.println(n); // DEBUG remove me!
            resultSet[i][0]=n.toPlainTextString().trim();
            resultSet[i][1]=null;
            Node c = n.getFirstChild();
            while( c!=null ) {
                if( c instanceof LinkTag ) {
                    resultSet[i][1] = ((LinkTag) c).getLink();
                    break;
                }
                c = c.getNextSibling();
            }
    
            System.out.println(i+" text :"+resultSet[i][0]); // DEBUG remove me!
            System.out.println(i+" link :"+resultSet[i][1]); // DEBUG remove me!
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to scrape some content from an HTML page. I'm using libxml2
I'm trying to parse HTML content using HTMLParser and with the help of it
Using the Python Documentation I found the HTML parser but I have no idea
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
I am trying to get a value out of a HTML page using the
I'm looking for a good html parser like HtmlAgilityPack (open-source .NET project: http://www.codeplex.com/htmlagilitypack ),
I am attempting to pull some information from my tnsnames file using regex. I
I'm using node-htmlparser to manipulate some HTML. Once I've manipulated the DOM object, how
Using the http://www.ifans.com/forums/showthread.php?t=132024 post from another question i am allowing the user to enter
Was following this Simple libxml2 HTML parsing example, using Objective-c, Xcode, and HTMLparser.h and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.