java解析sina視頻_java

來源:互聯網
上載者:User

用一個例子來說明,比如這個http://video.sina.com.cn/v/b/75314002-1648211320.html。
用Firefox開啟,開啟firebug,得到如下資訊。

其中在這個請求

http://v.iask.com/v_play.php?vid=75314002&uid=1648211320&pid=478&tid=&plid=4001&prid=ja_7_3485822616&referrer=&ran=0.2936802236363292&r=video.sina.com.cn

中得到的響應有我們想要的xml資訊,其中vid就是上面紅色部分,uid後面的都可以忽略,我們在瀏覽器中直接輸入http://v.iask.com/v_play.php?vid=75314002還是可以得到相同的資訊。自此解析的思路就變清晰了,在視頻連結中提取出vid,使用http://v.iask.com/v_play.php?vid=得到xml檔案,解析xml檔案就可以得到真實視頻地址了。

下面的是解析xml的代碼,使用sax解析xml。首先定義xml reader。

複製代碼 代碼如下:

package hdu.fang.parser;

import hdu.fang.model.Video;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLSaxReader extends DefaultHandler {
    private List<Video> videos = null;
    private Video video = null;
    private Long timeLength = null;
    private String tag = null;

    @Override
    public void startDocument() throws SAXException {
        videos = new ArrayList<Video>();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if ("durl".equals(qName)) {
            video = new Video();
        }

        tag = qName;
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if ("durl".equals(qName)) {
            videos.add(video);
            video = null;
        }
        tag = null;
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (tag != null) {
            String data = new String(ch, start, length);
            if ("timelength".equals(tag)) {
                timeLength = Long.valueOf(data);
            } else if ("order".equals(tag)) {
                video.setOrder(Integer.valueOf(data));
            } else if ("url".equals(tag)) {
                video.setUrl(data);
            } else if ("length".equals(tag)) {
                video.setLength(Integer.valueOf(data));
            }
        }
    }

     
    public List<Video> getVideos() {
        return videos;
    }

    public long getLength() {
        return timeLength;
    }
}

Video類是我自己定義的一個資料model。在主函數裡我們只要調用sax工廠執行個體化解析器就行了。

複製代碼 代碼如下:

SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
XMLSaxReader reader = new XMLSaxReader();
InputStream in_withcode = new ByteArrayInputStream(
    xml.getBytes("UTF-8"));//xml就是剛得到的xml檔案,類型String
sp.parse(in_withcode, reader);
videos=reader.getVideos();//得到Video List
timeLength=reader.getLength();//得到視頻長度
System.out.println(videos);

在xml檔案裡還有很多其他資訊,都可以解析出來,看自己需要了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.