how to access childnode which is increment by1 from nodelist in android

196 views Asked by At

i want to retreive data from xml file using nodelist but problem is i have image attribute which is increment how to get this attribute how to access this here i m going to put my xml file so u can understand easily

here see image attribute which is increment by 1 in this have photo_url i want to access tham how to access that photo_url

my.xml

<?xml version="1.0"?>
 <datas>
<places>
<place_name>newyork,us</place_name>
<images>
<image1>
    <image_id>3</image_id>
    <photo_url>http://192.168.1.21/pictastic/Application/uploads76/1337004413.jpg</photo_url>
    <image_url>http://192.168.1.21/pictastic/Application/uploads/1337004413.jpg</image_url>
    </image1>
<image2>
    <image_id>4</image_id>
    <photo_url>http://192.168.1.21/pictastic/Application/uploads76/1337004534.jpg</photo_url>
    <image_url>http://192.168.1.21/pictastic/Application/uploads/1337004534.jpg</image_url>
</image2>
<image3>
     <image_id>5</image_id>
     <photo_url>http://192.168.1.21/pictastic/Application/uploads76/1337006024.jpg</photo_url>
     <image_url>http://192.168.1.21/pictastic/Application/uploads/1337006024.jpg</image_url>
</image3>
</images>
</places>
<places>
<place_name>newzeland,abc</place_name>
<images>
<image1>
     <image_id>2</image_id>
     <photo_url>http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg</photo_url>
     <image_url>http://192.168.1.21/pictastic/Application/uploads/1337004360.jpg</image_url>
</image1>
<image2>
     <image_id>46</image_id>
 <photo_url>http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg</photo_url>
     <image_url>http://192.168.1.21/pictastic/Application/uploads/1337004360.jpg</image_url>
</image2>
</images>
     </places>
    <places>
    <place_name>5, 7</place_name>
    <images>
    <image1>
        <image_id>42</image_id>
        <photo_url>http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg</photo_url>
        <image_url>http://192.168.1.21/pictastic/Application/uploads/1337057023.jpg</image_url>
    </image1>
       </images>
            </places>

myactivity.java

     XMLParser parser = new XMLParser();
   String xml = parser.getXmlFromUrl(XURL); // getting XML
   Document doc = parser.getDomElement(xml); // getting DOM element
   NodeList nl = doc.getElementsByTagName(KEY_PLACES);
   NodeList nplacename=doc.getElementsByTagName(KEY_PLACENAME); 
   NodeList nimages=doc.getElementsByTagName(KEY_IMAGES);
   NodeList nimage=doc.getElementsByTagName(KEY_IMAGE);
   NodeList nimageid=doc.getElementsByTagName(KEY_IMAGEID);
   NodeList nimageurl=doc.getElementsByTagName(KEY_IMAGEURL);
   public void imageload()
{
     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    // looping through all item nodes <item>
     HashMap<String, String> map = new HashMap<String, String>();


    for (int i = 0; i < nl.getLength(); i++) 
    {
        if(nl.item(i).getNodeType()==Node.ELEMENT_NODE)
        {         
        NodeList nimages = nl.item(i).getChildNodes();
        Element e = (Element) nl.item(i);

        map.put(KEY_PLACENAME, parser.getValue(e, KEY_PLACENAME));

        for(int j = 0; j < nimages.getLength(); j++) 
        {  
            if (nimages.item(j).getNodeType() == Node.ELEMENT_NODE)
            {           
                //NodeList nimage=nimages.item(j).getChildNodes();
                Element eimages = (Element)nimages.item(j);
                 map.put(KEY_IMAGEURL, parser.getValue(e, KEY_IMAGEURL));   
                Log.e("Nimage","Nimage"+nimage);
                 mylist.add(map); 
                 Log.e("Mylist","Format----->"+mylist);

                    for(int k=0;k<nimage.getLength();k++)
                    {
                        Log.e("Nimage","Nimage"+nimage.getLength());
                        nimage.getLength();
                        Log.e("Nimage","Nimage"+nimage.getLength());
                        if (nimage.item(k).getNodeType() == Node.ELEMENT_NODE)
                        { 
                        //   NodeList nimageurl=nimage.item(k).getChildNodes();
                           Element eimage=(Element)nimage.item(k);
                           map.put(KEY_IMAGEURL, parser.getValue(e, KEY_IMAGEURL));
                           mylist.add(map); 
                        }

                    }

                }

            }
        }   
        mylist.add(map); 
        }

but when i access this is gives me wrong output

on log cat i print this

Logcat output

    05-21 11:49:36.357: D/dalvikvm(669): GC_FOR_MALLOC freed 3480 objects / 203712 bytes in 46ms
05-21 11:49:36.417: E/Nimage(669): Nimageorg.apache.harmony.xml.dom.NodeListImpl@44facb00
05-21 11:49:36.417: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004413.jpg, place_name=1, 4}]
05-21 11:49:36.417: E/Nimage(669): Nimageorg.apache.harmony.xml.dom.NodeListImpl@44facb00
05-21 11:49:36.417: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004413.jpg, place_name=1, 4}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004413.jpg, place_name=1, 4}]
05-21 11:49:36.417: E/Nimage(669): Nimageorg.apache.harmony.xml.dom.NodeListImpl@44facb00
05-21 11:49:36.427: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg, place_name=2, 4}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg, place_name=2, 4}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg, place_name=2, 4}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg, place_name=2, 4}]
05-21 11:49:36.427: E/Nimage(669): Nimageorg.apache.harmony.xml.dom.NodeListImpl@44facb00
05-21 11:49:36.427: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg, place_name=2, 4}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg, place_name=2, 4}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg, place_name=2, 4}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg, place_name=2, 4}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004360.jpg, place_name=2, 4}]
05-21 11:49:36.427: E/Nimage(669): Nimageorg.apache.harmony.xml.dom.NodeListImpl@44facb00
05-21 11:49:36.427: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}]
05-21 11:49:36.427: E/Nimage(669): Nimageorg.apache.harmony.xml.dom.NodeListImpl@44facb00
05-21 11:49:36.438: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337057023.jpg, place_name=5, 7}]
05-21 11:49:36.438: E/Nimage(669): Nimageorg.apache.harmony.xml.dom.NodeListImpl@44facb00
05-21 11:49:36.438: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}]
05-21 11:49:36.438: E/Nimage(669): Nimageorg.apache.harmony.xml.dom.NodeListImpl@44facb00
05-21 11:49:36.447: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337034609.jpg, place_name=Denmark, Eu}]
05-21 11:49:36.447: E/Nimage(669): Nimageorg.apache.harmony.xml.dom.NodeListImpl@44facb00
05-21 11:49:36.457: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}]
05-21 11:49:36.457: E/Nimage(669): Nimageorg.apache.harmony.xml.dom.NodeListImpl@44facb00
05-21 11:49:36.477: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}]
05-21 11:49:36.477: E/Mylist(669): Format----->[{photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}, {photo_url=http://192.168.1.21/pictastic/Application/uploads76/1337004257.jpg, place_name=Ahmadabad, IN}]
1

There are 1 answers

1
Konstantin Pribluda On

Better solution would be to use cleaner XML in form ... In this case you will be able to use pull paerser and databinding like XStream to easily turn your xml into java beans.

If fixing XML structure is not an option, you may just gel all the child nodes from node with node.getChildNodes() and filter this list by type and name.

PS: usind DOM for xml parsing is so 00s - we have pull parsing now which consimes much less memory and easier to hanldle