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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:58:35+00:00 2026-06-17T04:58:35+00:00

I have a cities.txt file placed in my res/raw folder. Inside it contains the

  • 0

I have a cities.txt file placed in my res/raw folder. Inside it contains the following.

<div class="state">Alabama</div>
<ul><li><a href="http://auburn.org">auburn</a></li>
<li><a href="http://bham.org">birmingham</a></li> </ul>

<div class="state">Alaska</div>
<ul><li><a href="http://anchorage.org">anchorage</a></li>
<li><a href="http://fairbanks.org">fairbanks</a></li></ul>

<div class="state">Arizona</div>
<ul><li><a href="http://flagstaff.org">flagstaff</a></li>
<li><a href="http://mohave.org">mohave county</a></li></ul>

I want to grab the cities for the state Alabama and display it on a ListView. The ouput should be like this.

auburn

birmingham

My current code grabs all the six cities and displays them on the ListView instead. This is my code.

package com.example.readfile;

import java.io.InputStream;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Cities extends Activity {

    ListView listUSCities;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.city_layout);
        listUSCities = (ListView) findViewById(R.id.listcities);

        new MyTask().execute();

    }

    class MyTask extends AsyncTask<Void, Void, ArrayList<String>> {

        ArrayList<String> arr_linkText = new ArrayList<String>();

        @Override
        protected ArrayList<String> doInBackground(Void... params) {

            Document doc;

            try {
                Resources res = getResources();
                InputStream in_s = res.openRawResource(R.raw.cities);

                byte[] b = new byte[in_s.available()];
                in_s.read(b);

                // Parsing using Jsoup starts here
                doc = Jsoup.parse(new String(b));

                // Parsing the states
                Elements links = doc.select("div");
                for (Element link : links) {
                    if (link.text().contains("Alabama")) {

                        // Extracting the cities
                        Elements cities = doc.select("a");
                        for (Element city : cities) {
                            arr_linkText.add(city.text());
                        }

                    }

                }

            } catch (Exception e) {
                // e.printStackTrace();

            }

            return arr_linkText; // << retrun ArrayList from here
        }

        @Override
        protected void onPostExecute(ArrayList<String> result) {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    Cities.this, android.R.layout.simple_list_item_1,
                    android.R.id.text1);
            for (String temp_result : result) {

                adapter.add(temp_result);
            }
            listUSCities.setAdapter(adapter);
        }

    }

}

How can I extract the cities only for that specified state?

How do I stop parsing the file after I extracted the cities to optimize speed?

The actual cities.txtcontains more information, I only provided a sample. I will appreciate your help. Thank you!

  • 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-06-17T04:58:36+00:00Added an answer on June 17, 2026 at 4:58 am

    That is an odd structure for an HTML document. The <div> is used just for the header, and the list is off by itself. Seeing as you trimmed the actual document, this may or may not work. The elements you are after are in the ul element following your div, so you need to go to the next sibling and search there. This will only work if there are no other elements between your div and ul elements.

    Elements links = doc.select("div");
    for (Element link : links) {
        if (link.text().contains("Alabama")) {
           // Extracting the cities in the list that is next in the DOM
            Elements cities = link.nextElementSibling().select("a");
            for (Element city : cities) {
    
                arr_linkText.add(city.text());
            }
    }
    

    }

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

Sidebar

Related Questions

I have the following City class. Each city object contains a dictionary which keys
I have two tables: attractions and cities. Attractions contains a column called city, which
I have CreateDiscountViewByUser discountViewByUser it contains a list of cities that are chosen by
I have a text file that reads: Left Behind,Lahaye,F,7,11.25 A Tale of Two Cities,Dickens,F,100,8.24
Say I have the following situation: I have some cities with buildings. Each city
I have states who have many cities (belongs_to :state) who have many businesses (belongs_to
I am trying to do 2 things: I have a text file ( Books.txt
My database has following structure: Every Region ( CountryStates ) can have many cities
I have cities: A, B, C, D, E How can I generate an initial
I have a list of cities, and i want a user can choose one

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.