`
sammyfun
  • 浏览: 1163517 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

本文转载自http://xdom.blogbus.com/logs/63153339.html

阅读更多
首先导入Ext.ux.tree.XmlTreeLoader.js

/*!
* Ext JS Library 3.2.0
* Copyright(c) 2006-2010 Ext JS, Inc.
* licensing@extjs.com
* http://www.extjs.com/license
*/
Ext.ns('Ext.ux.tree');

/**
* @class Ext.ux.tree.XmlTreeLoader
* @extends Ext.tree.TreeLoader
*

A TreeLoader that can convert an XML document into a hierarchy of {@link Ext.tree.TreeNode}s.
* Any text value included as a text node in the XML will be added to the parent node as an attribute
* called innerText.  Also, the tag name of each XML node will be added to the tree node as
* an attribute called tagName.

*

By default, this class expects that your source XML will provide the necessary attributes on each
* node as expected by the {@link Ext.tree.TreePanel} to display and load properly.  However, you can
* provide your own custom processing of node attributes by overriding the {@link #processNode} method
* and modifying the attributes as needed before they are used to create the associated TreeNode.

* @constructor
* Creates a new XmlTreeloader.
* @param {Object} config A config object containing config properties.
*/
Ext.ux.tree.XmlTreeLoader = Ext.extend(Ext.tree.TreeLoader, {
    /**
     * @property  XML_NODE_ELEMENT
     * XML element node (value 1, read-only)
     * @type Number
     */
    XML_NODE_ELEMENT : 1,
    /**
     * @property  XML_NODE_TEXT
     * XML text node (value 3, read-only)
     * @type Number
     */
    XML_NODE_TEXT : 3,

    // private override
    processResponse : function(response, node, callback){
        var xmlData = response.responseXML;
        var root = xmlData.documentElement || xmlData;

        try{
            node.beginUpdate();
            node.appendChild(this.parseXml(root));
            node.endUpdate();

            if(typeof callback == "function"){
                callback(this, node);
            }
        }catch(e){
            this.handleFailure(response);
        }
    },

    // private
    parseXml : function(node) {
        var nodes = [];
        Ext.each(node.childNodes, function(n){
            if(n.nodeType == this.XML_NODE_ELEMENT){
                var treeNode = this.createNode(n);
                if(n.childNodes.length > 0){
                    var child = this.parseXml(n);
                    if(typeof child == 'string'){
                        treeNode.attributes.innerText = child;
                        treeNode.text = child;
                    }else{
                        treeNode.appendChild(child);
                    }
                }
                nodes.push(treeNode);
            }
            else if(n.nodeType == this.XML_NODE_TEXT){
                var text = n.nodeValue.trim();
                if(text.length > 0){
                    return nodes = text;
                }
            }
        }, this);

        return nodes;
    },

    // private override
    createNode : function(node){
        var attr = {
            tagName: node.tagName
        };

        Ext.each(node.attributes, function(a){
            attr[a.nodeName] = a.nodeValue;
        });

        this.processAttributes(attr);

        return Ext.ux.tree.XmlTreeLoader.superclass.createNode.call(this, attr);
    },

    /*
     * Template method intended to be overridden by subclasses that need to provide
     * custom attribute processing prior to the creation of each TreeNode.  This method
     * will be passed a config object containing existing TreeNode attribute name/value
     * pairs which can be modified as needed directly (no need to return the object).
     */
    processAttributes: Ext.emptyFn
});

//backwards compat
Ext.ux.XmlTreeLoader = Ext.ux.tree.XmlTreeLoader;

然后添加针对XML文件格式的子类:

Ext.app.TestLoader = Ext.extend(Ext.ux.tree.XmlTreeLoader, {
    processAttributes : function(attr){
        if(attr.tagName=="category"){
            attr.text = attr.name;
            //attr.iconCls = '';
            attr.loaded = true;
            attr.expanded = false;
            if(attr.isValid != "true"){
             attr.hidden = true;
            }
        }
        else if(attr.tagName=="select"){
            //attr.iconCls = '';
            attr.leaf = true;
        }
    }
});

Tree中数据的载入:

var tree = new Ext.tree.TreePanel({
    renderTo: 'item',
       width: 500,
          height: 500,
    autoScroll:true,
    rootVisible: true,
          root: new Ext.tree.AsyncTreeNode({
              text: 'Root,
              expanded: true
          }),
          loader: new Ext.app.TestLoader({
              dataUrl:'data.xml'
          })
   });
分享到:
评论

相关推荐

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

     本文共分五大部分。分别是XML快速入门,XML的概念,XML的术语,XML的实现,XML的实例分析。最后附录介绍了XML的相关资源。作者站在普通网页设计人员的角度,用平实生动的语言,向您讲述XML的方方面面,帮助你拨开...

    YuNetsurf v3.0.0 for D6-XE10 HTML5 CSS 解析

    XDOM tree: Feasability study limited by XDOM restrictions. Fast, efficient, and low memory usage. CSS Parser and Selection Engine Parse CSS, good and bad. Apply CSS rules to DOM nodes: CSS2 and CSS...

    Delphi操作XML

    3. **OpenXML 解析器**:这是一个开源解析器,源代码位于 `xdom.pas` 单元中,可以通过官方网站获取最新的版本。 #### 五、解析器之间的比较 1. **微软的解析器**:性能较好,但在某些情况下可能存在兼容性问题,...

    xwiki-rendering-syntax-xdomxml10-6.3-rc-1.zip

    "xdomxml10"可能是指XDOM(XML Document Object Model)的一个特定版本,用于处理和呈现XML数据。在XWiki中,这样的语法扩展可能用于改进或扩展wiki文档的呈现方式。 【描述】中提到的"**h2o-2.zip**"是另一个开源...

    DOJO源代码

    这些控件基于 dojo/_base/xdom 和 dojo/dom-class 等模块,实现了丰富的样式和交互功能。通过 `dojo/parser`,我们可以将 HTML 中的标记解析成对应的 Dijit 控件。 ### 3. 数据绑定与数据存储 Dojo 提供了 `dojo/...

    Windows_Server_2008_AD架构-第02部分_创建管理域用户与组、只读域控制器RODC

    - 配置用户的登录名(UPN),这通常采用电子邮件地址的形式,例如 `emily@xdom.com`。其中 `emily` 是用户名,`xdom.com` 是域名称。 - 设置密码策略,包括是否要求用户更改初始密码、是否允许用户更改密码等。 ###...

    计算机网络-组织单位与委派控制-图文.pptx

    假设要在 xdom.biz.tw 域建立「总管理处」、「电脑事业处」和「业务处」等 3 个组织单位,之后因组织异动,将业务处改为隶属於总管理处,并更名为业务部,最后又删除电脑事业处。 委派控制 ------------- 委派控制...

    delphi面向对象(delphi框架)

    总体而言,从这部分内容来看,xDOM框架旨在为Delphi开发者提供一套完整的面向对象的解决方案,不仅涵盖了数据模型的构建,还包括了数据的存储、检索和显示,以及事件处理和数据流管理。这使得开发者能够更专注于业务...

    stock management(application with dutch comments)

    uses xdom_3_1

Global site tag (gtag.js) - Google Analytics