Minions
Minions

Reputation: 5477

Get image URL from XML tag using Jsoup

I'm trying to get an image URL that in XML file, but i can't !

Other Tags like "Description, title, etc" gotten successfully ! XML File:

<DOC>
<DOCNO>annotations/01/1001.eng</DOCNO>
<TITLE>Godchild Gustavo Javier Cuchiparte Umaginga</TITLE>
<DESCRIPTION>a dark-skinned boy wearing a black cap;</DESCRIPTION>
<NOTES></NOTES>
<LOCATION>Quilotoa, Ecuador</LOCATION>
<DATE>April 2002</DATE>
<IMAGE>images/01/1001.jpg</IMAGE>
<THUMBNAIL>thumbnails/01/1001.jpg</THUMBNAIL>
</DOC>

My code:

Elements titles = doc.getElementsByTag("TITLE");
Elements descriptions = doc.getElementsByTag("DESCRIPTION");
Elements img = doc.getElementsByTag("IMAGE");                    
String Imgurl=img.get(0).text(); 
System.out.println(Imgurl);//EMPTY !!

Upvotes: 0

Views: 503

Answers (1)

Yassin Hajaj
Yassin Hajaj

Reputation: 21975

Like you can see here, if JSoup parse it as a HTML file, your IMAGE tag is being modified and becomes <img>.

Here is how to parse an XML file properly :

Document doc = Jsoup.parse(yourXMLstring,"",Parser.xmlParser());

EDIT

It does work like this :

public static void main(String[] args) {
    String xml = "<DOC><DOCNO>annotations/01/1001.eng</DOCNO><TITLE>Godchild Gustavo Javier Cuchiparte Umaginga</TITLE><DESCRIPTION>a dark-skinned boy wearing a black cap;</DESCRIPTION><NOTES></NOTES><LOCATION>Quilotoa,Ecuador</LOCATION><DATE>April 2002</DATE><IMAGE>images/01/1001.jpg</IMAGE><THUMBNAIL>thumbnails/01/1001.jpg</THUMBNAIL></DOC>";
    Document doc = Jsoup.parse(xml,"",Parser.xmlParser());
    String image = doc.select("IMAGE").first().text();
    String image2 = doc.getElementsByTag("IMAGE").get(0).text();
    System.out.println(image);
    System.out.println(image2);
}

Output

images/01/1001.jpg
images/01/1001.jpg

If it does not work for you, you certainly have another problem. It does not lay in your code.

Upvotes: 1

Related Questions