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 6959167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:14:13+00:00 2026-05-27T15:14:13+00:00

I am currently using JTidy to parse an HTML document and fetch a collection

  • 0

I am currently using JTidy to parse an HTML document and fetch a collection of all anchor tags in the given HTML document. I then extract the value of each tag’s href attribute to come up with a collection of links on the page.

Unfortunately, these links can be expressed in a few different ways: some absolute (http://www.example.com/page.html), some relative (/page.html, page.html, or ../page.html). Even more, some can just be anchors (#paragraphA). When I visit my page in a browser, it knows automatically how to handle these different href values if I were to click the link, however if I were to follow one of these links retrieved from JTidy using an HTTPClient programatically, I first need to provide a valid URL (so e.g. I would first need to transform /page.html, page.html, and http://www.example.com/page.html to http://www.example.com/page.html).

Is there some built-in functionality, whether in JTidy or elsewhere, that can achieve this for me? Or will I need to create my own rules to transform these different URLs into an absolute URL?

  • 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-27T15:14:14+00:00Added an answer on May 27, 2026 at 3:14 pm

    The vanilla URL class might get you most of the way there, assuming you can work out which context to use. Here are some examples:

    package grimbo.url;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class TestURL {
        public static void main(String[] args) {
            // context1
            URL c1 = u(null, "http://www.example.com/page.html");
            u(c1, "http://www.example.com/page.html");
            u(c1, "/page.html");
            u(c1, "page.html");
            u(c1, "../page.html");
            u(c1, "#paragraphA");
    
            System.out.println();
    
            // context2
            URL c2 = u(null, "http://www.example.com/path/to/page.html");
            u(c2, "http://www.example.com/page.html");
            u(c2, "/page.html");
            u(c2, "page.html");
            u(c2, "../page.html");
            u(c2, "#paragraphA");
        }
    
        public static URL u(URL context, String url) {
            try {
                URL u = null != context ? new URL(context, url) : new URL(url);
                System.out.println(u);
                return u;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    

    Results in:

    http://www.example.com/page.html
    http://www.example.com/page.html
    http://www.example.com/page.html
    http://www.example.com/page.html
    http://www.example.com/../page.html
    http://www.example.com/page.html#paragraphA
    
    http://www.example.com/path/to/page.html
    http://www.example.com/page.html
    http://www.example.com/page.html
    http://www.example.com/path/to/page.html
    http://www.example.com/path/page.html
    http://www.example.com/path/to/page.html#paragraphA
    

    As you can see, there are some results that aren’t what you want. So maybe you try and parse the URL using new URL(value) first, and if that results in a MalformedURLException you could try relative to a context URL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I am trying to clean up an HTML file using JTidy, convert it
I'm currently using DPack as this adds a Collapse All Projects option to the
Currently using Telerik ASP .NET MVC Controls version 2011.2.712 Hello all, I am trying
Im currently using PHP to fetch results from a mysql db. Im also displaying
Im currently using jquery to link all td's to the edit link but i
Hi all I am currently using FLEX version 3.0 It just take a long
currently using PHP5 with htmlMimeMail 5 ( http://www.phpguru.org/static/mime.mail.html ) to send HTML e-mail communications.
Currently using System.Web.UI.WebControls.FileUpload wrapped in our own control. We have licenses for Telerik. I
Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted
Currently using Google Analytics as a supplement to our paid tracking software, but neither

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.