2

I'm trying to get some name form class attribute within a website html page by using Jsoup Library, The problem is that i'm getting the elements by class using getElementsByClass("name") and store it into a string variable and the result coming like this "mike andro rob banks maria gerardo louis....etc". but what i want is to separate the individual names and store them into array. the following is the code snippet:

public String processText(String htmlPage) {

    Document html = Jsoup.parse(htmlPage);
    String names = html.body().getElementsByClass("name").text();
    return names;
}

More information:

The source page is an html page and i am saving the full html code in a string and then process the string to extract only the Elements under the class="name"

htmlPage which i am passing to processText method is similar to the following:

<div class="name">
       Rob Kardashian
      </div>
     </div>
    </a>
   </div>
     <div class="channelListEntry">
    <a href="/zayn_malik">
     <div class="image">
      <img src="http://cdn.posh24.com/images/:profile/014cf47ca44daf8f44a3e0720929ee327" alt="Zayn Malik"/>
     </div>
     
      
          <div class="info">
      <div class="status-container">
       <div class="position">4</div>
        
        <div class="img pos"></div>
        <div class="value">+12</div>
             
      </div>
      <div class="name">
       Zayn Malik
      </div>
     </div>
    </a>
   </div>
     <div class="channelListEntry">
    <a href="/kanye_west">
     <div class="image">
      <img src="http://cdn.posh24.com/images/:profile/03f352f71ffab135cd81821eb190d4832" alt="Kanye West"/>
     </div>
     
      
          <div class="info">
      <div class="status-container">
       <div class="position">5</div>
        
        <div class="img pos"></div>
        <div class="value">+16</div>
             
      </div>
      <div class="name">
       Kanye West
      </div>
     </div>
    </a>
   </div>
     <div class="channelListEntry">
    <a href="/kendall_jenner">
     <div class="image">
      <img src="http://cdn.posh24.com/images/:profile/066d5c02547c4357f1bc5f633c68f4085" alt="Kendall Jenner"/>
     </div>
Pavneet_Singh
  • 34,557
  • 5
  • 43
  • 59
Emad Jaber
  • 57
  • 1
  • 7

1 Answers1

0

you can simply use split function to get an array from string

String arr[]=names.trim().split("\\s");

plus if you have spaces and tab combined between name then use

  String arr[]=names.split("\\s+");

Update:

      ArrayList<String>  name=new ArrayList<String>();
      for (Element output: html.body().getElementsByClass("name")) {
          name.add(output.text());
          }

example link

Output :

enter image description here

link to convert list to array

Community
  • 1
  • 1
Pavneet_Singh
  • 34,557
  • 5
  • 43
  • 59