簡體   English   中英

如何在jsoup中提取屬性

[英]How to extract an attribute in jsoup

如何從以下XML標簽提取url = ""部分。 我在Android中使用Jsoup

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

看我的代碼:

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:  
                    }

這是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");}

您通過標簽名稱搜索,它將獲得帶有您輸入標簽的元素.. i使用get(0)表示它可能是鏈接的第一個元素。或..可能是它是唯一元素..使用索引正如您在鏈接.. attr中看到其順序一樣:要獲取該元素的屬性,它的返回字符串..

祝好運 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM