如何从android中的nodelist访问增加1的子节点

时间:2012-05-21 06:24:33

标签: android arraylist xmlnodelist

我想使用nodelist从xml文件中检索数据但问题是我有图像属性,这是增量如何获取此属性如何访问此 这里我要放我的xml文件 所以你可以轻松理解

这里看到图片属性,增加1,这里有photo_url我想访问tham如何访问该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); 
        }

但是当我访问它时会给我错误的输出

on log cat i打印此

  

Logcat输出

    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 个答案:

答案 0 :(得分:0)

更好的解决方案是在表单中使用更清晰的XML ... 在这种情况下,您将能够像XStream一样使用pull paerser和数据绑定来轻松地将xml转换为java bean。

如果不能选择修复XML结构,则可以从节点中凝胶化所有子节点 使用node.getChildNodes()并按类型和名称过滤此列表。

PS:用于xml解析的usind DOM是如此00s - 我们现在有拉解析,这个解析更少 记忆,更容易hanldle