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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:23:06+00:00 2026-05-28T00:23:06+00:00

I need to extract the text between two HTML tags and store it in

  • 0

I need to extract the text between two HTML tags and store it in a string. An example of the HTML I want to parse is as follows:

<div id=\"swiki.2.1\"> THE TEXT I NEED </div>

I have done this in Java using the pattern (swiki\.2\.1\\\")(.*)(\/div) and getting the string I want from the group $2. However this will not work in android. When I go to print the contents of $2 nothing appears, because the match fails.

Has anyone had a similar problem with using regex in android, or is there a better way (non-regex) to parse the HTML page in the first place. Again, this works fine in a standard java test program. Any help would be greatly appreciated!

  • 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-28T00:23:07+00:00Added an answer on May 28, 2026 at 12:23 am

    For HTML-parsing-stuff I always use HtmlCleaner: http://htmlcleaner.sourceforge.net/

    Awesome lib that works great with Xpath and of course Android. 🙂

    This shows how you can download an XML from URL and parse it to get a certain value from an XML attribute (also shown in the docs):

    public static String snapFromHtmlWithCookies(Context context, String xPath, String attrToSnap, String urlString,
                        String cookies) throws IOException, XPatherException {
                String snap = "";
    
                // create an instance of HtmlCleaner
                HtmlCleaner cleaner = new HtmlCleaner();
    
                // take default cleaner properties
                CleanerProperties props = cleaner.getProperties();
    
                props.setAllowHtmlInsideAttributes(true);
                props.setAllowMultiWordAttributes(true);
                props.setRecognizeUnicodeChars(true);
                props.setOmitComments(true);
    
                URL url = new URL(urlString);
    
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
    
                // optional cookies
                connection.setRequestProperty(context.getString(R.string.cookie_prefix), cookies);
                connection.connect();
    
                // use the cleaner to "clean" the HTML and return it as a TagNode object
                TagNode root = cleaner.clean(new InputStreamReader(connection.getInputStream()));
    
                Object[] foundNodes = root.evaluateXPath(xPath);
    
                if (foundNodes.length > 0) {
                        TagNode foundNode = (TagNode) foundNodes[0];
                        snap = foundNode.getAttributeByName(attrToSnap);
                }
    
                return snap;
        }
    

    Just edit it for your needs. 🙂

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

Sidebar

Related Questions

I have a string of text that contains html, and I need to extract
Problem: Extract all html between two headers including the headers html. The header text
I need to extract text from an HTML file using C#. I am trying
I need to extract the text (characters and numbers) from a multiline string. Everything
I am trying to extract the text out of nested tags for example the
I need to animate the text color of a custom control between two colors,
I have a string that covers several lines. I need to extract the text
I use Jericho HTML Parser 3.1. I need to extract text from html, handle
I need to extract some text from a string, then replace that text with
I am having real problems trying to extract the text between a HTML header

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.