Xml解析媒体标签值没有进入android?

时间:2015-03-04 12:18:51

标签: android xml-parsing

我实现xml解析,但是当我访问媒体标签时,图像值没有显示。我已经实现了下面的检查代码不适用于媒体ID和url.you可以检查下面图像中的xml数据

enter image description here

public static final String X_PF_CONTENT_IMPORT="pf_content_import";
    public static final String X_SPOT="spot";
    public static final String X_ID="id";
        public static final String X_VALID="valid";
        public static final String X_TEXT="text";
            public static final String X_HEADER="header";
            public static final String X_BODY="body";
        public static final String X_MEDIA="media";
            public static final String X_IMAGE="image"; 

XmlParsing xparser= new XmlParsing();           
            String xml = xparser.getXmlFromUrl(url); // getting XML
            Document doc = xparser.getDomElement(xml); // getting DOM element   
            NodeList nspot = doc.getElementsByTagName(X_SPOT);
            Log.e("Nodename","--->"+nspot.getLength());
            for(int i=0;i<nspot.getLength();i++){
                    Node n=nspot.item(i);
                    Element eLemenSpot = (Element)n;
                    String id=eLemenSpot.getAttribute(X_ID);
                    String valid=eLemenSpot.getElementsByTagName(X_VALID).item(0).getTextContent();
                    Log.e("Id","--->"+id);
                    Log.e("Valid","--->"+valid);

                    NodeList text=eLemenSpot.getElementsByTagName(X_TEXT);

                    Node ntext=text.item(0);
                    Element elementText=(Element)ntext;
                    String header=elementText.getElementsByTagName(X_HEADER).item(0).getTextContent();
                    String body=elementText.getElementsByTagName(X_BODY).item(0).getTextContent();

                    Log.e("Header","--->"+header);
                    Log.e("Body","--->"+body);

                    NodeList media=eLemenSpot.getElementsByTagName(X_MEDIA);
                    for(int k=0;k<media.getLength();k++){
                        Node nmedia=media.item(k);
                        Element elementMedia=(Element)nmedia;
                        String image_id=elementMedia.getAttribute("id");
                        String image_url=elementMedia.getElementsByTagName(X_IMAGE).item(k).getTextContent();
                        Log.e(" Media Id", "---->"+image_id);
                        Log.e("Media Url", "---->"+image_url);
                    }

            }
  

logcat的

03-05 00:36:11.513: E/Nodename(14195): --->2
03-05 00:36:11.513: E/Id(14195): --->1
03-05 00:36:11.513: E/Valid(14195): --->2015-03-05T11:34:59.337278+01:00
03-05 00:36:11.523: E/Header(14195): --->OPTISCH GANZ VERÃNDERT
03-05 00:36:11.523: E/Body(14195): --->BACK TO THE ROOTS: MANDY CAPRISTO PUNKTET MIT DUNKLEN HAAREN
03-05 00:36:11.523: E/Media Id(14195): ---->
03-05 00:36:11.523: E/Media Url(14195): ---->http://content3.promiflash.de/article-images/infoscreen1/mandy-capristo-mit-dunklen-haaren.jpg
03-05 00:36:11.523: E/Id(14195): --->2
03-05 00:36:11.523: E/Valid(14195): --->2015-03-05T11:34:59.337278+01:00
03-05 00:36:11.523: E/Header(14195): --->OPTISCH GANZ VERÃNDERT
03-05 00:36:11.523: E/Body(14195): --->BACK TO THE ROOTS: MANDY CAPRISTO PUNKTET MIT DUNKLEN HAAREN
03-05 00:36:11.533: E/Media Id(14195): ---->
03-05 00:36:11.533: E/Media Url(14195): ---->http://content3.promiflash.de/article-images/infoscreen2/mandy-capristo-mit-dunklen-haaren.jpg

2 个答案:

答案 0 :(得分:1)

请试试这个

NodeList nodeList = document.getElementsByTagName("image");
for(int i=0,size= nodeList.getLength(); i<size; i++) {
     Log.e("",nodeList.item(i).getAttributes().getNamedItem("id").getNodeValue());
     Log.i("",nodeList.item(i).getTextContent());
}

答案 1 :(得分:0)

我需要创建imagenode列表

NodeList media=eLemenSpot.getElementsByTagName(X_MEDIA);
                for(int k=0;k<media.getLength();k++){
                    Node nmedia=media.item(k);
                    Element elementMedia=(Element)nmedia;
                    NodeList nimage=elementMedia.getElementsByTagName(X_IMAGE);
                    for(int j=0;j<nimage.getLength();j++){
                        Log.e(" Media Id", "---->"+nimage.item(j).getAttributes().getNamedItem(X_ID).getNodeValue());
                        Log.e("Media Url", "---->"+nimage.item(j).getTextContent());

                    }
                }