1

I am developing an android app which use data from external sources like severs. Data are retrieved using DOM parser. My actual problem is

 <enclosure url="http://news.oneindia.in/img/2014/07/15-tripura-map-600.jpg" type="image/jpeg" length="810000"/>

from the tag enclosure I want to retrieve the url for my app .

When I am retrieving the enclosure tag I am getting null .
So how could i get that data from inside the tag ?

<item>
  <title>Modi in Brazil, Israel airstrike: Follow News In Brief: July 15</title>
  <link>http://news.oneindia.in/india/follow-news-brief-july-15-1482982.html</link>
  <guid>http://news.oneindia.in/india/follow-news-brief-july-15-1482982.html</guid>
  <description>Bangalore, July 15: Follow news in brief for the day: 2:40 pm:&amp;nbsp;Lalu, Nitish to forge alliance for Bihar Assembly by-polls 2:22 pm:&amp;nbsp;World Cup 2014 champions German team arrives in home country, receives grand welcome 1:50 pm:&amp;nbsp;30 die of </description>
  <enclosure url="http://news.oneindia.in/img/2014/07/15-pm-narendramodi-brics.jpg" type="image/jpeg" length="810000"/>
  <pubDate>Tue, 15 Jul 2014 08:48:32 +0530</pubDate>
</item>
<item>
  <title>Heavy rain alert for Uttarakhand</title>
  <link>http://news.oneindia.in/india/heavy-rain-alert-uttarakhand-1482983.html</link>
  <guid>http://news.oneindia.in/india/heavy-rain-alert-uttarakhand-1482983.html</guid>
  <description>Lucknow/Dehradun, July 15: An alert has been sounded in Uttarakhand after incessant rains in many parts of the state for the last 48 hours, officials said Tuesday.The hill state is likely to experience very heavy rainfall Wednesday, the Met Office said. </description>
  <enclosure url="http://news.oneindia.in/img/2014/07/15-dehradun-map-600.jpg" type="image/jpeg" length="810000"/>
  <pubDate>Tue, 15 Jul 2014 08:48:06 +0530</pubDate>
</item>
<item>
  <title>Former US president Bill Clinton arrives in Jaipur</title>
  <link>http://news.oneindia.in/india/former-us-prez-bill-clinton-arrives-in-jaipur-1482985.html</link>
  <guid>http://news.oneindia.in/india/former-us-prez-bill-clinton-arrives-in-jaipur-1482985.html</guid>
  <description>Jaipur, July 15: Former US President Bill Clinton arrived here on Monday night on a tour during which he will visit a kitchen being run for schoolchildren by an NGO. Clinton landed with his delegation at the international airport at </description>
  <enclosure url="http://news.oneindia.in/img/2014/07/15-bill-clinton-latest.jpg" type="image/jpeg" length="810000"/>
  <pubDate>Tue, 15 Jul 2014 01:25:31 +0530</pubDate>
</item>

From above xml i want to extract contents inside enclosure.

Here is my java code

    public class Second_Listview extends Activity {

private GestureDetector gestureDetector;
// All static variables
// static final String URL = "http://ibnlive.in.com/ibnrss/top.xml";
String URL;
// XML node keys
static final String KEY_SONG = "item"; // parent node
static final String KEY_ID = "description";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "pubDate";
static final String KEY_DURATION = "link";
static final String KEY_DIS = "description";
 static final String KEY_THUMB_URL = "enclosure";

ListView list;
LazyAdapter adapter;
String descrption;
String image;
String main_title;

ProgressBar progress;

String KEY_IMAGE;
String imgurl;


// >>> in case of data connection failure

// flag for Internet connection status
Boolean isInternetPresent = false;

// Connection detector class
ConnectionDetector cd;



@Override
public void onCreate(Bundle savedInstanceState) {


     gestureDetector = new GestureDetector(
             new SwipeGestureDetector());

    //reciving intent data

    Intent intent = getIntent();
     main_title = getIntent().getExtras().getString("title_key").trim();

    super.onCreate(savedInstanceState);
    setTitle(main_title);   // setting title bar title
    setContentView(R.layout.main);
    progress=(ProgressBar)findViewById(R.id.progressBarMain);
    progress.setVisibility(View.GONE);
    //reciving intent data



    String page_id = getIntent().getExtras().getString("webpage_key");  //reciving intent data

    URL = page_id.trim(); // trimming data to avoid space

    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL);// getting XML from URL

    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {


        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);

        descrption = parser.getValue(e, KEY_THUMB_URL);


        if (e.getNodeName().contains("enclosure") && e.hasAttributes()){
                imgurl= e.getAttribute("url").toString();


        }

        System.out.println("VALUE INSIDE>>>>>>>>>>> "+url+" <<<< DDDD"+ descrption );

        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
        map.put(KEY_DIS, parser.getValue(e, KEY_DIS));
        map.put(KEY_IMAGE, imgurl);

the value i get inside descrption is null

Nikhil
  • 911
  • 15
  • 28
  • What code to you use to "retrieve the enclosure tag"? It's hard to help you just by reading your question. – f1sh Jul 14 '14 at 07:49

1 Answers1

0

For getting the value of url attribute of enclosure tag you can do something similar as specified in the post Parsing attribute in XML with DOM parser. Below is one sample.

NamedNodeMap attributes = fstElmnt.getAttributes();
Node enclosureItem = attributes.getNamedItem("url");

It would have been great if you have posted your parsing code, then could have posted example which you could directly use within your code.

==================== Edit based on your code =========================

Element e = (Element) nl.item(i);

//if (e.getNodeName().contains("enclosure") && e.hasAttributes()){
   String url = e.getAttribute("url").toString();
//}
Community
  • 1
  • 1
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
  • i just updated my question with detailed explanation , please provide me a good solution – Nikhil Jul 15 '14 at 09:50
  • Thanks for your help , but its still getting Null value in url . What should i do ? – Nikhil Jul 16 '14 at 05:57
  • can you pl update your code above so i can see your updated implementation of the logic? – Rajen Raiyarela Jul 16 '14 at 06:12
  • are you getting any kind of exception or error? is it going inside the if condition for getting the url attribute value? – Rajen Raiyarela Jul 16 '14 at 06:39
  • no error or exception showing , i put a toast inside the if condition but its not working . i think the if condtion is not working – Nikhil Jul 16 '14 at 06:43
  • see i just edited my answer and removed if block. can you pl check by updating your code and see it is working? check this url might be helpful for you http://theopentutorials.com/tutorials/android/xml/android-parsing-xml-with-children-and-attributes-using-dom/ – Rajen Raiyarela Jul 16 '14 at 06:59
  • you need to debug yourself now ... are you getting XML in String XML? are you getting nodelist in nl? – Rajen Raiyarela Jul 16 '14 at 10:04
  • After a long try i made it , used my own logic with the help of DOM parser – Nikhil Jul 17 '14 at 04:11
  • Could you please help me to use Async Task in these Class , any feedback would be helpful – Nikhil Jul 18 '14 at 06:57
  • check out this post, check for my answer, http://stackoverflow.com/questions/24731838/android-loop-to-upload-list-images-one-by-one/24801946?noredirect=1#comment38497510_24801946, it is for uploading of image but you can modify the same classes for your scenario by changing the parameters in interface class, as well as AysncTask class and handling the response in your activity class. In your case, you can use onPostExecute method to send back response to caller activity. in link i provided it is done through onProgressUpdate. – Rajen Raiyarela Jul 18 '14 at 07:08
  • could you help me to create one ? – Nikhil Jul 18 '14 at 09:30