前言
正文
一、目標
以多級目錄分類方式在在線視頻欄目下添加電視臺。
二、主要代碼
電視臺的地址目前是存在XML文件裏,那麼本文代碼主要就是解析XML的數據了。
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.content.Context;
import com.nmbb.oplayer.po.OnlineVideo;
/** 從XML讀取電視臺節目 */
public class XmlReaderHelper {
/** 獲取所有電視分類 */
public static ArrayList<OnlineVideo> getAllCategory(final Context context) {
ArrayList<OnlineVideo> result = new ArrayList<OnlineVideo>();
DocumentBuilderFactory docBuilderFactory = null;
DocumentBuilder docBuilder = null;
Document doc = null;
try {
docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFactory.newDocumentBuilder();
// xml file 放到 assets目錄中的
doc = docBuilder.parse(context.getResources().getAssets()
.open("online.xml"));
// root element
Element root = doc.getDocumentElement();
NodeList nodeList = root.getElementsByTagName("category");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);// category
OnlineVideo ov = new OnlineVideo();
NamedNodeMap attr = node.getAttributes();
ov.title = attr.getNamedItem("name").getNodeValue();
ov.id = attr.getNamedItem("id").getNodeValue();
ov.category = 1;
ov.level = 2;
ov.is_category = true;
result.add(ov);
// Read Node
}
} catch (IOException e) {
} catch (SAXException e) {
} catch (ParserConfigurationException e) {
} finally {
doc = null;
docBuilder = null;
docBuilderFactory = null;
}
return result;
}
/** 讀取分類下所有電視地址 */
public static ArrayList<OnlineVideo> getVideos(final Context context,
String categoryId) {
ArrayList<OnlineVideo> result = new ArrayList<OnlineVideo>();
DocumentBuilderFactory docBuilderFactory = null;
DocumentBuilder docBuilder = null;
Document doc = null;
try {
docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFactory.newDocumentBuilder();
// xml file 放到 assets目錄中的
doc = docBuilder.parse(context.getResources().getAssets()
.open("online.xml"));
// root element
Element root = doc.getElementById(categoryId);
if (root != null) {
NodeList nodeList = root.getChildNodes();
for (int i = 0, j = nodeList.getLength(); i < j; i++) {
Node baseNode = nodeList.item(i);
if (!"item".equals(baseNode.getNodeName()))
continue;
String id = baseNode.getFirstChild().getNodeValue();
if (id == null)
continue;
OnlineVideo ov = new OnlineVideo();
ov.id = id;
Element el = doc.getElementById(ov.id);
if (el != null) {
ov.title = el.getAttribute("title");
ov.icon_url = el.getAttribute("image");
ov.level = 3;
ov.category = 1;
NodeList nodes = el.getChildNodes();
for (int m = 0, n = nodes.getLength(); m < n; m++) {
Node node = nodes.item(m);
if (!"ref".equals(node.getNodeName()))
continue;
String href = node.getAttributes()
.getNamedItem("href").getNodeValue();
if (ov.url == null) {
ov.url = href;
} else {
if (ov.backup_url == null)
ov.backup_url = new ArrayList<String>();
ov.backup_url.add(href);
}
}
if (ov.url != null)
result.add(ov);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} finally {
doc = null;
docBuilder = null;
docBuilderFactory = null;
}
return result;
}
}
請移步#Taocode(SVN):
項目地址:http://code.taobao.org/p/oplayer
SVN地址:http://code.taobao.org/svn/oplayer/
四、Vitamio公告
正式建立Vitamio開發者聯盟QQ羣!羣號爲:246969281注意:目前僅接受已經開發基於Vitamio產品的開發者申請加入,申請理由請填寫產品的名稱和鏈接!
注意更新至2012-07-09發佈的新版SDK:這裏。
五、參考
結束
本文是新入手Macbook Pro上寫的第一篇文章,諸多不習慣,仍然在一天內成功丟棄鼠標,離IOS又近一步了:) 系列文章並不強調用某種技術,但儘可能涉及到多種技術,只有充分了解各種技術才能在適當的時候使用合適的技術。先實現後優化,不必一步到位糾結於每個細節。