简体   繁体   English

如何在jsoup中提取属性

[英]How to extract an attribute in jsoup

How do I extract the url = "" part from the following XML-Tag. 如何从以下XML标签提取url = ""部分。 I am using Jsoup , in Android. 我在Android中使用Jsoup

<enclosure type="image/jpg" url="EXTRACT THIS" length="123456" />

See my code: 看我的代码:

Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            // Get all <item> tags.
            NodeList nl = doc.getElementsByTagName("item");
            int length = nl.getLength();


            for (int i = 0; i < length; i++) {
                Node currentNode = nl.item(i);
                RSSItem _item = new RSSItem();

                NodeList nchild = currentNode.getChildNodes();
                int clength = nchild.getLength();

                // Get the required elements from each Item
                for (int j = 0; j < clength; j = j + 1) {

                    Node thisNode = nchild.item(j);
                    String theString = null;
                    String nodeName = thisNode.getNodeName();

                    //NodeList test = nchild.item(j).getChildNodes();

                    if(j<=3){
                        theString = nchild.item(j).getFirstChild().getNodeValue();
                    }
                    if("enclosure".equals(nodeName)){
                        //HAVE TO GET URL HERE from ATTRIBUTE:  
                    }

This is the XML: 这是XML:

view-source:http://www.skysports.com/rss/0,20514,11661,00.xml
Document doc = Jsoup.connect(link).get();
        Elements elements= doc
                .getElementsByTag("enclosure");
    for(int i=0;i<elements.getsize();i++){
        String url=elements.get(i).attr("href");}

You search by Tag Name and it will get elements That have Tag You Entered .. i Use get(0) to indicate that it may be first element of your link .. OR .. may be It is The only element .. Use index as You see its order in link .. attr : To get attribute of in that element It`s Return String .. 您通过标签名称搜索,它将获得带有您输入标签的元素.. i使用get(0)表示它可能是链接的第一个元素。或..可能是它是唯一元素..使用索引正如您在链接.. attr中看到其顺序一样:要获取该元素的属性,它的返回字符串..

Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM