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

  • Home
  • SEARCH
  • 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 6872057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:52:49+00:00 2026-05-27T03:52:49+00:00

I am new in parsing HTML using Java. What I want to do is

  • 0

I am new in parsing HTML using Java. What I want to do is to get the text between tags but those tags contains some optional attributes.
for example, I have the folowing string

HelloWorld!

I want to extract the text of the second cell only which is “World!”. (and it has diffrent attributes from “Hello”)

What I have found here so far is:

import java.io.*;
import java.net.URL;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;

public class HtmlParseDemo {

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

        Reader reader = new StringReader("<tr><td align=\"center\" width=\"408\"><font color=\"#000000\">"
                + "Hello </font></td><td align=\"center\" width=\"275\"><font color=\"#0000FF\">World! "
               + "</font></td></tr>");
        HTMLEditorKit.Parser parser = new ParserDelegator();
        parser.parse(reader, new HTMLTableParser(), true);
        reader.close();
    }
}

class HTMLTableParser extends HTMLEditorKit.ParserCallback {

    private boolean encounteredATableRow = false;

    public void handleText(char[] data, int pos) {
        if (encounteredATableRow) {
            System.out.println(new String(data));
        }
    }

    public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
        if (t == HTML.Tag.TD) {
            encounteredATableRow = true;
        }
    }

    public void handleEndTag(HTML.Tag t, int pos) {
        if (t == HTML.Tag.TD) {
            encounteredATableRow = false;
        }
    }
}

Output:

Hello
World!

It output all the text regardles the attributes.

Any ideas please?

  • 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-27T03:52:50+00:00Added an answer on May 27, 2026 at 3:52 am

    I did it and it worked:

    import java.io.*;
    import java.net.URL;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    import javax.swing.text.html.parser.*;
    
    public class HtmlParseDemo {
    
        public static void main(String[] args) throws Exception {
    
            Reader reader = new StringReader("<tr><td align=\"center\" width=\"408\"><font color=\"#000000\">"
                    + "Hello </font></td><td align=\"center\" width=\"275\"><font color=\"#0000FF\">World! "
                   + "</font></td></tr>");
            HTMLEditorKit.Parser parser = new ParserDelegator();
            parser.parse(reader, new HTMLTableParser(), true);
            reader.close();
        }
    }
    class HTMLTableParser extends HTMLEditorKit.ParserCallback {
    
        private boolean encounteredATableRow = false;
    
        public void handleText(char[] data, int pos) {
            if (encounteredATableRow) {
                System.out.println(new String(data));
            }
        }
    
        public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
            String name1 = (String) a.getAttribute(HTML.Attribute.WIDTH);
            if (t == HTML.Tag.TD) {
                if (name1 != null && name1.equalsIgnoreCase("275") == true) {
    //                System.out.println(name1);
                    encounteredATableRow = true;
                }
            }
        }
    
        public void handleEndTag(HTML.Tag t, int pos) {
            if (t == HTML.Tag.TD) {
                encounteredATableRow = false;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing some data using DOMDocument after fetching HTML file using curl. The codes
Hi I am writng some pdf by parsing html to it using itextsharp and
Parsing HTML / JS codes to get info using PHP. www.asos.com/Asos/Little-Asos-Union-Jack-T-Shirt/Prod/pgeproduct.aspx?iid=1273626 Take a look
I'm new to F# and functional and am working on some HTML parsing code.
I'm new to DOM parsing in PHP: I have a HTML file that I'm
Extended Backus–Naur Form: EBNF I'm very new to parsing concepts. Where can I get
I am using HtmlAgility pack for parsing the html page. I am able to
I am using the following code for parsing dom document but at the end
I want to parse with XmlSlurper a HTML document which I read using HTTPBuilder.

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.