Update SrcJySet.js

pull/1/head
jevons0411 3 years ago
parent 60a73fe4e3
commit 0a5c711660
  1. 145
      SrcJySet.js

@ -2,107 +2,74 @@
function SRCSet() {
let xmlhtml = request('http://f2dcj6.com/sapi?ac=videolist&pg=1&t=3');
function loadXml(str) {
if (str == null) {
return null;
}
var doc = str;
try{
doc = createXMLDOM();
doc.async = false;
doc.loadXML(str);
}catch(e){
doc = $.parseXML(str);
function getXmlDocumentByXmlString(xmlString) {
var xmlDoc = null;
if (window.DOMParser) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlString, "text/xml");
} else {
//IE
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlString);
}
return doc;
return xmlDoc;
}
//xml数据转成json对象的数据
function xmlTojson(xmlObj, nodename, isarray){
var obj = $(xmlObj);
var itemobj = {};
var nodenames = "";
var getAllAttrs = function(node){//递归解析xml 转换成json对象
var _itemobj = {};
var notNull = false;
var nodechilds = node.childNodes;
var childlenght = nodechilds.length;
var _attrs = node.attributes;
var firstnodeName = "#text";
try{
firstnodeName = nodechilds[0].nodeName;
}catch(e){
}
if((childlenght > 0 && firstnodeName != "#text") || _attrs.length > 0){
var _childs = nodechilds;
var _childslength = nodechilds.length;
var _fileName_ = "";
if(undefined != _attrs){
var _attrslength = _attrs.length;
for(var i = 0; i < _attrslength; i++){//解析xml节点属性
var attrname = _attrs[i].nodeName;
var attrvalue = _attrs[i].nodeValue;
_itemobj[attrname] = attrvalue;
}
}
for (var j = 0; j < _childslength; j++) {//解析xml子节点
var _node = _childs[j];
var _fildName = _node.nodeName;
if("#text" == _fildName){break;};
if(_itemobj[_fildName] != undefined){//如果有重复的节点需要转为数组格式
if(!(_itemobj[_fildName] instanceof Array)){
var a = _itemobj[_fildName];
_itemobj[_fildName] = [a];//如果该节点出现大于一个的情况 把第一个的值存放到数组中
}
}
var _fildValue = getAllAttrs(_node);
try{
_itemobj[_fildName].push(_fildValue);
}catch(e){
_itemobj[_fildName] = _fildValue;
_itemobj["length"] = 1;
}
}
function convertToJSON(xmlDoc) {
//准备JSON字符串和缓存(提升性能)
var jsonStr = "";
var buffer = new Array();
buffer.push("{");
//获取xml文档的所有子节点
var nodeList = xmlDoc.childNodes;
generate(nodeList);
/**
* 中间函数用于递归解析xml文档对象并附加到json字符串中
* @param node_list xml文档的的nodeList
*/
function generate(node_list) {
for (var i = 0; i < node_list.length; i++) {
var curr_node = node_list[i];
//忽略子节点中的换行和空格
if (curr_node.nodeType == 3) {
continue;
}
//如果子节点还包括子节点,则继续进行遍历
if (curr_node.childNodes.length > 1) {
buffer.push("\"" + curr_node.nodeName + "\": {");
generate(curr_node.childNodes);
} else {
var firstChild = curr_node.childNodes[0];
if (firstChild != null) {
//nodeValue不为null
buffer.push("\"" + curr_node.nodeName + "\":\"" + firstChild.nodeValue + "\"");
} else {
_itemobj = (node.textContent == undefined) ? node.text : node.textContent;
//nodeValue为null
buffer.push("\"" + curr_node.nodeName + "\":\"\"");
}
return _itemobj;
};
if(nodename){
nodenames = nodename.split("/")
}
for(var i = 0;i < nodenames.length; i++){
obj = obj.find(nodenames[i]);
}
$(obj).each(function(key, item){
if(itemobj[item.nodeName] != undefined){
if(!(itemobj[item.nodeName] instanceof Array)){
var a = itemobj[item.nodeName];
itemobj[item.nodeName] = [a];
}
itemobj[item.nodeName].push(getAllAttrs(item));
}else{
if(nodenames.length > 0){
itemobj[item.nodeName] = getAllAttrs(item);
if (i < (node_list.length - 2)) {
buffer.push(",");
} else {
itemobj[item.firstChild.nodeName] = getAllAttrs(item.firstChild);
}
}
});
if(nodenames.length > 1){
itemobj = itemobj[nodenames[nodenames.length-1]];
break;
}
if(isarray && !(itemobj instanceof Array) && itemobj != undefined){
itemobj = [itemobj];
}
return itemobj;
//添加末尾的"}"
buffer.push("}");
}
var xmlObj = loadXml(xmlhtml);
var jsonData = xmlTojson(xmlObj, 'userList/user', true);
log(jsonData)
jsonStr = buffer.join("");
return jsonStr;
}
let xmljosn = convertToJSON(getXmlDocumentByXmlString(xmlhtml));
log(xmljosn);
addListener("onClose", $.toString(() => {

Loading…
Cancel
Save