<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        XmlUtilsJS操作XML工具類_javascript技巧

        來源:懂視網 責編:小采 時間:2020-11-27 20:44:36
        文檔

        XmlUtilsJS操作XML工具類_javascript技巧

        XmlUtilsJS操作XML工具類_javascript技巧:demo用了點extjs的東西,主要是為了打印json數組出來。 js code(XmlUtils.js): 代碼如下: /**/ function XmlUtils (config) { /*定義私有屬性*/ this.isIE = 。(window.attachEvent && !window.opera); this.
        推薦度:
        導讀XmlUtilsJS操作XML工具類_javascript技巧:demo用了點extjs的東西,主要是為了打印json數組出來。 js code(XmlUtils.js): 代碼如下: /**/ function XmlUtils (config) { /*定義私有屬性*/ this.isIE = 。(window.attachEvent && !window.opera); this.

        demo用了點extjs的東西,主要是為了打印json數組出來。
        js code(XmlUtils.js):
        代碼如下:
        /**/
        function XmlUtils (config) {
        /*定義私有屬性*/
        this.isIE = !!(window.attachEvent && !window.opera);
        this.init();
        if(config) {
        this.dataType = config.dataType == 'json' ? 'json' : 'array';
        if(config.xmlPath) this.loadXml(config.xmlPath);
        }
        }
        XmlUtils.prototype = {
        xmlDoc : null,
        xmlPath : null,
        dataType : null,
        /**
        * 初始化
        */
        init : function () {
        if (this.isIE) {
        var activexArr = ["MSXML4.DOMDocument", "MSXML3.DOMDocument", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XmlDom"];
        for(i=0; itry{
        this.xmlDoc = new ActiveXObject(activexArr[i]);
        }catch(e){}
        }
        } else {
        this.xmlDoc = document.implementation.createDocument("", "", null);
        }
        },

        /**
        * 加載xml文件,參數:
        * @param {string} xmlPath:加載的xml文件路徑;
        * @return {Object} true 正常加載; false 加載失敗
        */
        loadXml : function (xmlPath) {
        try {
        this.xmlDoc.async = false;
        this.xmlDoc.load(xmlPath);
        this.xmlPath = xmlPath;
        return true;
        } catch (e) {
        return false;
        }
        },

        /**
        * 加載XML字符串
        * @param {Object} XMLString
        */
        loadXmlString: function(xmlString) {
        if (this.isIE) {
        this.xmlDoc.loadXML(xmlString);
        } else {
        var parser = new DOMParser();
        this.XMLDoc = parser.parseFromString(xmlString, "text/xml");
        }
        },

        /**
        * 判斷節點的是否有子節點
        * @param {Object} node
        * @return {Object} 有子節點則返回true,否則返回false
        */
        hasChildNodes : function (node) {
        return node.hasChildNodes();
        },

        /**
        * 判斷節點的是否有屬性
        * @param {Object} node
        * @return {Object} 有屬性則返回true,否則返回false
        */
        hasAttributes : function (node) {
        return (node.attributes.length > 0) ? true : false;
        },

        /**
        * 判斷節點的是否是文本節點,包括帶CDATA區段的文本節點
        * @param {Object} node
        * @return {Object} 是文本節點則返回true,否則返回false
        */
        isTextNode : function (node) {
        var type = this.getNodeType(node);
        return (type == 3 || type == 4) ? true : false;
        },

        /**
        * 返回根節點
        * @return {Object} 根節點
        */
        getRoot : function () {
        return this.xmlDoc.documentElement;
        },

        /**
        * 返回節點的第一個子節點,沒有參數則返回根節點的第一個子節點
        * @param {Object} node
        * @return {Object} 節點的第一個子節點
        */
        getFirstChild : function (node) {
        return node ? node.firstChild : this.getRoot().firstChild;
        },

        /**
        * 返回節點的最后子節點,沒有參數則返回根節點的第一個子節點
        * @param {Object} node
        * @return {Object} 節點的最后一個子節點
        */
        getLastChild : function (node) {
        return node ? node.lastChild : this.getRoot().lastChild;
        },

        /**
        * 返回節點的下一個節點,沒有參數則返回根節點的第一個子節點
        * @param {Object} node
        * @return {Object} 節點的下一個節點
        */
        getNextNode : function (node) {
        return node ? node.nextSibling : null;
        },

        /**
        * 返回節點的上一個節點,沒有參數則返回根節點的第一個子節點
        * @param {Object} node
        * @return {Object} 節點的上一個節點
        */
        getPreviousNode : function (node) {
        return node ? node.previousSibling : null;
        },

        /**
        * 返回節點的子節點,沒有參數則返回null
        * @param {Object} node
        * @return {Object} 節點所有子節點
        */
        getChildNodes : function (node) {
        return (node && this.hasChildNodes(node)) ? node.childNodes : null;
        },

        /**
        * 返回節點的父節點,沒有參數則返回null
        * @param {Object} node
        * @return {Object} 節點父節點
        */
        getParentNode : function (node) {
        return node ? node.parentNode : null;
        },

        /**
        * 根據節點名返回節點數組文本值,參數:
        * @param {string或object} nodeName:節點名稱;
        * @return {object} 節點存在返回節點數組;節點不存在則返回null。
        */
        getNodesTextByName : function (nodeNames) {
        return nodeNames ? (this.dataType == 'json' ? this.getJsonNodesTextByName(nodeNames) : this.getArryNodesTextByName(nodeNames)) : null;
        },

        /**
        * 根據節點名返回節點普通數組文本值,參數:
        * @param {string或object} nodeName:節點名稱;
        * @return {object} 節點存在返回節點普通數組。
        */
        getArryNodesTextByName : function (nodeNames) {
        var rs = [];
        //返回普通數組格式
        switch (typeof(nodeNames)) {
        case 'string':
        var nodes = this.getNodesByTagName(nodeNames);
        for (var i = 0; i < nodes.length; i++) {
        rs.push(nodes[i].text);
        }
        break;
        case 'object':
        var subRs;
        var nodes;
        for (var i = 0; i < nodeNames.length; i++) {
        nodes = this.getNodesByTagName(nodeNames[i]);
        subRs = [];
        for (var j = 0; j < nodes.length; j++) {
        subRs.push(nodes[j].text);
        }
        rs.push(subRs);
        }
        break;
        }
        return rs;
        },

        /**
        * 根據節點名返回節點JSON數組文本值,參數:
        * @param {string或object} nodeName:節點名稱;
        * @return {object} 節點存在返回節點JSON數組;節點不存在則返回null。
        */
        getJsonNodesTextByName : function (nodeNames) {
        var rs = null;
        //返回JSON數組格式
        switch (typeof(nodeNames)) {
        case 'string':
        eval('rs = {' + nodeNames + ':[]}');
        var nodes = this.getNodesByTagName(nodeNames);
        for (var i = 0; i < nodes.length; i++) {
        eval('rs.' + nodeNames + '.push({' + nodeNames + i + ': nodes[i].text})');
        }
        break;
        case 'object':
        rs = {};
        var nodes;
        for (var i = 0; i < nodeNames.length; i++) {
        eval('rs.' + nodeNames[i] + '=[]');
        nodes = this.getNodesByTagName(nodeNames[i]);
        for (var j = 0; j < nodes.length; j++) {
        eval('rs.' + nodeNames[i] + '.push({' + nodeNames[i] + j + ': nodes[j].text})');
        }
        }
        break;
        }
        return rs;
        },

        /**
        * 根據節點屬性得到節點,參數:
        * @param {String} key:屬性名,默認是id
        * @param {String} value:屬性值
        * @return {String} 符合條件的節點數組。
        */
        getNodesByAttribute : function (key, value) {
        key = key ? key : 'id';
        value = value ? value : '';
        return id ? this.xmlDoc.getElementById(id) : null;
        },

        /**
        * 根據節點名得到節點,參數:
        * @param {string} tagName:節點名稱
        * @return {string} 指定節點名字的和位置的節點或節點數組。
        */
        getNodesByTagName : function (tagName) {
        return tagName ? this.xmlDoc.getElementsByTagName(tagName) : null;
        },

        /**
        * 根據節點路徑返回第index個節點,參數:
        * @param {string} xPath:節點路徑
        * @param {number}index:要索引的位置,為空或0則返回所有查找到的節點。
        * @return {string} 指定節點名字的和位置的節點或節點數組。
        */
        getNodesByXpath : function (xPath, index) {
        if (!xPath) return null;
        var nodes = this.xmlDoc.selectNodes(xPath);
        var len = nodes.length;
        if(!index || index > len || index < 0) return nodes;
        for(var i=0; iif(i == index - 1) return nodes[i];
        }
        },

        /**
        * 得到指定節點文本,參數:
        * @param {object} node:節點
        * @return {string} 節點文本,為空則返回null
        */
        getText : function (node) {
        return node ? node.text : null;
        },

        /**
        * 得到指定節點名稱,參數:
        * @param {object} node:節點
        * @return {string} 節點名稱,為空則返回null
        */
        getTagName : function (node) {
        return node ? node.nodeName : null;
        },

        /**
        * 返回節點類型,參數:
        * @param {object} node:節點
        * @return {string} 節點類型,為空則返回null
        * 1-element
        * 2-attribute
        * 3-text
        * 4-cdata
        * 5-entity reference
        * 6-entity
        * 7-pi (processing instruction)
        * 8-comment
        * 9-document
        * 10-document type
        * 11-document fragment
        * 12-notation
        */
        getNodeType : function (node) {
        return node ? node.nodeType : null;
        },

        /**
        * 創建節點,參數:
        * @param {string} nodeName:節點名稱,必填
        * @param {string} text:節點文本,可為空
        * @param {Object} attributes:屬性值-JSON數組,可為空,例:{id:'id001',name:'name001'}
        * @param {Object} node:要增加子節點的節點,為空則返回新建的節點
        * @param {Boolean} cdata:是否生成帶有CDATA區段的節點,true:生成,false:不生成
        * @return {Object} 創建的節點,有異常則返回null
        */
        createNode: function(nodeName, text, attributes, node, cdata) {
        if (this.isIE) {
        //創建子接點
        var childNode = this.xmlDoc.createElement(nodeName);
        //創建文本節點
        var textNode = cdata == true ? this.xmlDoc.createCDATASection(text) : this.xmlDoc.createTextNode(text);
        childNode.appendChild(textNode);
        //添加屬性
        for (var i in attributes) {
        this.createAttribute(childNode,i,attributes[i]);
        };

        return node ? node.appendChild(childNode) : childNode;
        } else {
        alert('FF創建節點再說.');
        return null;
        }
        },

        /**
        * 創建帶CDATA區段的節點,參數:
        * @param {string} nodeName:節點名稱,必填
        * @param {string} text:節點文本,可為空
        * @param {Object} attributes:屬性值-JSON數組,可為空,例:{id:'id001',name:'name001'}
        * @param {Object} node:要增加子節點的節點,為空則返回新建的節點
        */
        createCDATANode: function(nodeName, text, attributes, node) {
        this.createNode(nodeName, text, attributes, node, true);
        },

        /**
        * 創建節點屬性,參數:
        * @param {Object} node:節點,必填
        * @param {String} key:屬性名,必填
        * @param {Object} value:屬性值,必填
        * @param {Object} node:返回新增屬性的節點
        * @return {Object} 增加屬性的節點,有異常則返回null
        */
        createAttribute: function(node, key, value) {
        if (this.isIE) {
        if(!key) return;
        var attr = this.xmlDoc.createAttribute(key);
        attr.value = value ? value : "";
        node.setAttributeNode(attr);
        return node;
        } else {
        alert('FF創建節點再說.');
        return node;
        }
        return null;
        },

        /**
        * 把節點加到根節點上,參數:
        * @param {Object} node:節點
        * @return {Object} 有異常則返回null
        */
        addNodeToRoot: function(node) {
        if(!node) return null;
        this.getRoot().appendChild(node);
        return node;
        },

        /**
        * 把節點加到另外節點上,參數:
        * @param {Object} node:節點
        */
        addNode: function(node,childNode) {
        return (node && childNode) ? node.appendChild(childNode) : false;
        },

        /**
        * 從父節點移除節點自身,參數:
        * @param {Object} newNode:要替換的節點
        * @param {Object} oldNode:要被替換的節點
        */
        replaceChild: function(newNode, oldNode) {
        var parentNode = oldNode.parentNode;
        if(!newNode || !oldNode || !parentNode) return;
        parentNode.replaceChild(newNode, oldNode);
        },

        /**
        * 從父節點移除節點自身,參數:
        * @param {Object} node:要移除的節點
        */
        removeChild: function(node) {
        if(!node || !node.parentNode) return;
        node.parentNode.removeChild(node);
        },

        /**
        * 移除節點的所有子節點,參數:
        * @param {Object} node:父節點
        */
        removeChildNodes: function(node) {
        if (node && this.hasChildNodes(node)) {
        var childNodes = node.childNodes;
        for(var i = 0; i < childNodes.length; i++) {
        node.removeChild(childNodes[0]);
        }
        }
        },

        /**
        * 設置節點屬性值,不存在則新建,參數:
        * @param {Object} node:要設置的節點
        * @param {String} key:要設置的屬性名
        * @param {String} value:要設置的屬性值
        */
        setAttribute: function(node, key, value) {
        this.createAttribute(node, key, value);
        },

        /**
        * 設置文本節點的文本,參數:
        * @param {Object} node:要設置的節點
        * @param {String} text:要設置的文本
        */
        setText: function(node, text) {
        if(this.isTextNode(node)) node.text = text;
        },

        /**
        * 在文本節點后面追加文本,參數:
        * @param {Object} node:要設置的節點
        * @param {String} text:要設置的文本
        */
        appendText: function(node, text) {
        if(this.isTextNode(node)) node.appendData(text);
        },


        /**
        * 輸出xml,為空則輸出根節點文本,參數:
        * @param {Object} node:要輸出的節點
        */
        toString: function(node) {
        node = node ? node : this.xmlDoc.documentElement;
        if (typeof node == 'string') return node;
        return this.isIE ? node.xml : new XMLSerializer().serializeToString(node);
        }
        }

        測試的xml文件(book.xml):
        代碼如下:



        西游記
        吳承恩


        紅樓夢
        曹雪芹


        三國演義

        施耐庵




        水滸傳
        羅貫中



        html code (test.html):
        代碼如下:



        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        XmlUtilsJS操作XML工具類_javascript技巧

        XmlUtilsJS操作XML工具類_javascript技巧:demo用了點extjs的東西,主要是為了打印json數組出來。 js code(XmlUtils.js): 代碼如下: /**/ function XmlUtils (config) { /*定義私有屬性*/ this.isIE = 。(window.attachEvent && !window.opera); this.
        推薦度:
        標簽: js 工具類 xml
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲国产精品VA在线看黑人| 亚洲真人无码永久在线| 亚洲国产av高清无码| 亚洲a一级免费视频| 久久精品国产亚洲麻豆| 久久99免费视频| 亚洲免费视频网站| 97在线视频免费公开观看| 亚洲成AV人片久久| 成年人视频在线观看免费| 午夜亚洲乱码伦小说区69堂| 亚洲乱码日产精品a级毛片久久| 一个人看的免费观看日本视频www 一个人看的免费视频www在线高清动漫 | 亚洲成在人线在线播放无码| 免费永久看黄在线观看app| 无人视频在线观看免费播放影院| 亚洲AV蜜桃永久无码精品| 少妇性饥渴无码A区免费| 亚洲欧洲日产国产综合网| h片在线免费观看| 亚洲国产午夜精品理论片在线播放 | 精品一区二区三区免费毛片爱| 亚洲国产中文在线二区三区免 | 亚洲福利视频一区二区| a视频在线观看免费| 亚洲人成在线精品| 亚洲成人影院在线观看| 午夜影院免费观看| 亚洲成a∧人片在线观看无码| 亚洲精品网站在线观看不卡无广告 | 亚洲喷奶水中文字幕电影| 国产高清免费的视频| 国产日韩久久免费影院| 亚洲三级在线播放| 亚洲精品tv久久久久久久久久| 免费播放一区二区三区| 国产精品亚洲综合天堂夜夜| 国产成人A人亚洲精品无码| 日本成人在线免费观看| 国产免费无码一区二区| 亚洲国产精品精华液|