Android-如何在android中获取xml内部标记的内容

时间:2016-07-20 14:46:31

标签: android xml-parsing news-feed domparser

我从url获得了一个包含以下标记的xml,   HTTP://www.ZZZZZZZZZZZZz.jpg'宽度=' 90'高度=' 62'> AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

我需要获取img src =' http://www.ZZZZZZZZZZZZz.jpg'内的链接。并且描述中的内容分别说明了AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA astring如何实现它。

我已经写了一段代码,但它没有拍摄图片的网址

private void ProcessXml(Document data) {
    if (data!=null){
       feedList=new ArrayList<>();
        Element root=data.getDocumentElement();
        Node channel=root.getChildNodes().item(1);
        NodeList items=channel.getChildNodes();
        for (int i=0;i<items.getLength();i++){
            Node currentChild=items.item(i);
            if (currentChild.getNodeName().equalsIgnoreCase("item")){
                GetterSetter getterSetter=new GetterSetter();
                NodeList itemchild=currentChild.getChildNodes();
                for (int j=0;j<itemchild.getLength();j++){
                    Node current=itemchild.item(j);
                    Log.d("aaa", "ProcessXml: ="+current.getTextContent());
                    if (current.getNodeName().equalsIgnoreCase("title")){
                        getterSetter.setTitle(current.getTextContent());
                    }
                    else            if(current.getNodeName().equalsIgnoreCase("description")){
                             getterSetter.setDescription(current.getTextContent());
                    }
                    else if (current.getNodeName().equalsIgnoreCase("link")){
                        getterSetter.setLink(current.getTextContent());
                    }
                    else if (current.getNodeName().equalsIgnoreCase("pubDate")){
                        getterSetter.setPubDate(current.getTextContent());
                    }else if (current.getNodeName().equalsIgnoreCase("enclosure url")){
                        String url=current.getAttributes().item(0).getTextContent();
                        getterSetter.setThumbnail(url);
                    }

                   }
                feedList.add(getterSetter);
                Log.d("qqq", "thumbnail: ="+getterSetter.getThumbnail());


            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

Jsoup库可能有所帮助。

你的build.gradle中的

dependencies {
    ...
    compile 'org.jsoup:jsoup:1.9.1'
    ...
}

解析如下:

String html = "<img src='http://www.ZZZZZZZZZZZZz.jpg' width='90' height='62'>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

Document doc = Jsoup.parse(html);
String imgUrl = doc.select("img").attr("src"); //http://www.ZZZZZZZZZZZZz.jpg
String description = doc.text(); //AAAAAAAAAAAA...