Java Analytics Sina Video _java

Source: Internet
Author: User
Tags xml reader

Use an example to illustrate, such as this http://video.sina.com.cn/v/b/75314002-1648211320.html.
With Firefox open, open Firebug, get the following information.

which in this request

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

The response we get is the XML information we want, in which the vid is the red section above, and the UID can be ignored, and we can enter http://v.iask.com/v_play.php?vid=75314002 directly in the browser or get the same information. From this analysis of the idea becomes clear, in the video link extracted vid, using http://v.iask.com/v_play.php?vid= to get the XML file, parsing XML file can get the real video address.

The following is the code that parses the XML, using Sax to parse the XML. First, define the XML reader.

Copy Code code as follows:

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 = 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;
}
}

The video class is a data model that I define myself. In the main function we just call the Sax factory instantiation parser.

Copy Code code as follows:

SAXParserFactory SF = Saxparserfactory.newinstance ();
SAXParser sp = Sf.newsaxparser ();
Xmlsaxreader reader = new Xmlsaxreader ();
InputStream In_withcode = new Bytearrayinputstream (
Xml.getbytes ("UTF-8"));//xml is just the resulting XML file, type string
Sp.parse (In_withcode, reader);
Videos=reader.getvideos ()//Get the video List
Timelength=reader.getlength ()//Get video length
SYSTEM.OUT.PRINTLN (VIDEOS);

There's a lot of other information in the XML file that you can parse out and see what you need.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.