user2579475
user2579475

Reputation: 1101

get <img> value from a string in java

I'm parsing data from a json file. Now, I've a data like this

String Content = <p><img class="alignleft size-full wp-image-56999" alt="abdullah" src="http://www.some.com/wp-content/uploads/2013/12/imageName.jpg" width="348" height="239" />Text</p>
<p>Text</p> <p>Text</p><p>The post <a href="Some URL">Some Text</a> appeared first on <a href="http://www.some.com">Some Webiste</a></p>

Now, I want to divide this string in two pieces. I want to get this URL from src.

http://www.some.com/wp-content/uploads/2013/12/imageName.jpg

and store it a variable. Also, I want to remove the last line The Post appeared... and store the text's in another variable.

So, the questions are:

  1. Is it possible to get that?
  2. If possible, how can I achieve that ?

Upvotes: 0

Views: 78

Answers (3)

AurA
AurA

Reputation: 12363

IN Java

Get a Document object

Document originalDoc = new SAXReader().read(new StringReader("<div>data</div>");

Then you can parse it.. (read this tutorial)

http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

In JavaScript to get attribute

var url = document.getElementsByTagName('img')[0].getAttribute('src');

In case if you have a string and you want a document object, use jquery

string stringValue = '<div>data</div>';
var myObject= $(stringValue); 

Upvotes: 3

Sage
Sage

Reputation: 15418

  1. Use String.substring(firstIndex, lastIndex) to get the link from src attribute
  2. learn to use a HTML parser like JSoup, will be useful in near future

Upvotes: 2

sanket
sanket

Reputation: 789

If its a well structured string you can parse it using any DOM parser and extract data from it...

Upvotes: 0

Related Questions