
function XmlDom(){
    var _root_str="zb";
    var _xmlParam = new ActiveXObject("Microsoft.XMLDOM");
    var _xpath_root="/"+_root_str;
    var _xpath_relative=_xpath_root;
    
    this.ROOT=_root_str;
    this.addNode=addNode;
    this.addNodeText=addNodeText;
    this.addNodeAndAttrib=addNodeAndAttrib;
    this.addNodeTextAndAttrib=addNodeTextAndAttrib;
    this.addNodeByXPath=addNodeByXPath;
    this.addNodeByRootXPath=addNodeByRootXPath;
    this.addNodeByRelativeXPath=addNodeByRelativeXPath;
    this.addNodeTextByXPath=addNodeTextByXPath;
    this.addNodeTextByRootXPath=addNodeTextByRootXPath;
    this.addNodeTextByRelativeXPath=addNodeTextByRelativeXPath;
    this.addNodeTextAndAttribByXPath=addNodeTextAndAttribByXPath;
    this.addNodeTextAndAttribByRootXPath=addNodeTextAndAttribByRootXPath;
    this.addNodeTextAndAttribByRelativeXPath=addNodeTextAndAttribByRelativeXPath;
    this.setAttribByXPath=setAttribByXPath;
    this.setAttribByRootXPath=setAttribByRootXPath;
    this.setAttribByRelativeXPath=setAttribByRelativeXPath;
    this.toXML=toXML;
    this.loadDom=loadDom;
    this.loadXML=loadXML;
    this.getNodeCountByXPath=getNodeCountByXPath;
    this.getNodeCountByRootXPath=getNodeCountByRootXPath;
    this.getNodeCountByRelativeXPath=getNodeCountByRelativeXPath;
    this.getRootNodeCount=getRootNodeCount;
    this.getNodeTextByXPath=getNodeTextByXPath;
    this.getNodeTextByRootXPath=getNodeTextByRootXPath;
    this.getNodeTextByRelativeXPath=getNodeTextByRelativeXPath;
    this.getNodeAttribByXPath=getNodeAttribByXPath;
    this.getNodeAttribByRootXPath=getNodeAttribByRootXPath;
    this.getNodeAttribByRelativeXPath=getNodeAttribByRelativeXPath;
    this.getNodeByXPath=getNodeByXPath;
    this.getNodeByRootXPath=getNodeByRootXPath;
    this.getNodeByRelativeXPath=getNodeByRelativeXPath;
    this.removeAllChildNode=removeAllChildNode;
    this.reset=reset;
    this.setRelativeXPath=setRelativeXPath;
    this.upLevelXPath=upLevelXPath;
    
    var _root_node=addRootNode();

    return this;

    function toXML(){
        return _xmlParam.xml;
    }

    function setRelativeXPath(v){
        _xpath_relative=getRealXPath(_xpath_relative,v);
        
        return _xpath_relative;
    }
    
    function getRealXPath(v1,v2){
        if((v1!=null)&&(v2!=null)){
            if((typeof(v1)=="string")&&(typeof(v2)=="string")){
                var t=v2.charAt(0);
                if(t!="/"){
                    v2="/"+v2;
                }
                
                v1=v1+v2;
            }
        }
        
        return v1;
    }
    
    function upLevelXPath(){
        var t=_xpath_relative.lastIndexOf("/");
        if(t!=-1){
            if(t!=0){
                _xpath_relative=_xpath_relative.substring(0,t-1);
            }
        }
        
        return _xpath_relative;
    }

    function addRootNode(){
        var oRoot;
        oRoot=_xmlParam.createNode("element",_root_str,"");
        _xmlParam.appendChild(oRoot);

        return oRoot;
    }

    function addNode(node_name){
        var oRoot;
        oRoot=_xmlParam.createNode("element",node_name,"");
        _root_node.appendChild(oRoot);
        return oRoot;
    }

    function addNodeText(node_name,node_value){
        var oRoot;
        oRoot=addNode(node_name);
        oRoot.text=node_value;
        return oRoot;
    }

    function addNodeAndAttrib(node_name,attrib_name,attrib_value){
        var oRoot;
        oRoot=_xmlParam.createNode("element",node_name,"");
        oRoot.setAttribute(attrib_name,attrib_value);
        _root_node.appendChild(oRoot);
        return oRoot;
    }

    function addNodeTextAndAttrib(node_name,node_value,attrib_name,attrib_value){
        var oRoot;
        oRoot=addNodeAndAttrib(node_name,attrib_name,attrib_value);
        oRoot.text=node_value;
        return oRoot;
    }

    function setAttribByXPath(xpath,attrib_name,attrib_value){
        var oNode=getNodeByXPath(xpath);
        oNode.setAttribute(attrib_name,attrib_value);
        return oNode;
    }
    
    function setAttribByRootXPath(xpath,attrib_name,attrib_value){
        xpath=getRealXPath(_xpath_root,xpath);
        return setAttribByXPath(xpath,attrib_name,attrib_value);
    }
    
    function setAttribByRelativeXPath(xpath,attrib_name,attrib_value){
        xpath=getRealXPath(_xpath_relative,xpath);
        return setAttribByXPath(xpath,attrib_name,attrib_value);
    }

    function addNodeTextByXPath(xpath,node_name,node_value){
        var oRoot=addNodeByXPath(xpath,node_name);
        oRoot.text=node_value;
        
        return oRoot;
    }
    
    function addNodeTextByRootXPath(xpath,node_name,node_value){
        xpath=getRealXPath(_xpath_root,xpath);
        return addNodeTextByXPath(xpath,node_name,node_value);
    }

    function addNodeTextByRelativeXPath(xpath,node_name,node_value){
        xpath=getRealXPath(_xpath_relative,xpath);
        return addNodeTextByXPath(xpath,node_name,node_value);
    }

    function addNodeByXPath(xpath,node_name){
        var oRoot;
        oRoot=_xmlParam.createNode("element",node_name,"");
        var oParent;
        oParent=_xmlParam.selectSingleNode(xpath);
        oParent.appendChild(oRoot);
        return oRoot;
    }
    
    function addNodeByRootXPath(xpath,node_name){
        xpath=getRealXPath(_xpath_root,xpath);
        return addNodeByXPath(xpath,node_name);
    }

    function addNodeByRelativeXPath(xpath,node_name){
        xpath=getRealXPath(_xpath_relative,xpath);
        return addNodeByXPath(xpath,node_name);
    }

    function addNodeTextAndAttribByXPath(xpath,node_name,node_value,attrib_name,attrib_value){
        var oRoot=addNodeAndAttribByXPath(xpath,node_name,attrib_name,attrib_value);
        var v=toString(node_value);
        oRoot.text=v;
        
        return oRoot;
    }
    
    function addNodeTextAndAttribByRootXPath(xpath,node_name,node_value,attrib_name,attrib_value){
        xpath=getRealXPath(_xpath_root,xpath);
        return addNodeTextAndAttribByXPath(xpath,node_name,node_value,attrib_name,attrib_value);
    }

    function addNodeTextAndAttribByRelativeXPath(xpath,node_name,node_value,attrib_name,attrib_value){
        xpath=getRealXPath(_xpath_relative,xpath);
        return addNodeTextAndAttribByXPath(xpath,node_name,node_value,attrib_name,attrib_value);
    }

    function addNodeAndAttribByXPath(xpath,node_name,attrib_name,attrib_value){
        var oRoot;
        oRoot=_xmlParam.createNode("element",node_name,"");
        oRoot.setAttribute(attrib_name,attrib_value);
        var oParent;
        oParent=_xmlParam.selectSingleNode(xpath);
        oParent.appendChild(oRoot);
        return oRoot;
    }
    
    function addNodeAndAttribByRootXPath(xpath,node_name,attrib_name,attrib_value){
        xpath=getRealXPath(_xpath_root,xpath);
        return addNodeAndAttribByXPath(xpath,node_name,attrib_name,attrib_value);
    }

    function addNodeAndAttribByRelativeXPath(xpath,node_name,attrib_name,attrib_value){
        xpath=getRealXPath(_xpath_relative,xpath);
        return addNodeAndAttribByXPath(xpath,node_name,attrib_name,attrib_value);
    }

    function loadDom(dom){
        _xmlParam=dom;
    }

    function loadXML(xml){
        _xmlParam.loadXML(xml);
    }

    //根据XPath获得节点的个数 
    function getNodeCountByXPath(xpath){
        var oNodeList;
        oNodeList=_xmlParam.selectNodes(xpath);
        return oNodeList.length;
    }
    
    function getNodeCountByRootXPath(xpath){
        xpath=getRealXPath(_xpath_root,xpath);
        return getNodeCountByXPath(xpath);
    }
    
    function getNodeCountByRelativeXPath(xpath){
        xpath=getRealXPath(_xpath_relative,xpath);
        return getNodeCountByXPath(xpath);
    }
    
    //获得根节点下的节点个数
    function getRootNodeCount(node_name){
        return getNodeCountByXPath("/"+_root_str+"/"+node_name);
    } 

    function getNodeTextByXPath(xpath){
        var oNode=_xmlParam.selectSingleNode(xpath);
        return oNode.text;
    }
    
    function getNodeTextByRootXPath(xpath){
        xpath=getRealXPath(_xpath_root,xpath);
        return getNodeTextByXPath(xpath);
    }

    function getNodeTextByRelativeXPath(xpath){
        xpath=getRealXPath(_xpath_relative,xpath);
        return getNodeTextByXPath(xpath);
    }

    function getNodeAttribByXPath(xpath,attrib_name){
        var oNode=_xmlParam.selectSingleNode(xpath);
        return oNode.getAttribute(attrib_name);
    }
    
    function getNodeAttribByRootXPath(xpath,attrib_name){
        xpath=getRealXPath(_xpath_root,xpath);
        return getNodeAttribByXPath(xpath,attrib_name);
    }

    function getNodeAttribByRelativeXPath(xpath,attrib_name){
        xpath=getRealXPath(_xpath_relative,xpath);
        return getNodeAttribByXPath(xpath,attrib_name);
    }

    function getNodeByXPath(xpath){
        return _xmlParam.selectSingleNode(xpath);
    }
    
    function getNodeByRootXPath(xpath){
        xpath=getRealXPath(_xpath_root,xpath);
        return getNodeByXPath(xpath);
    }
    
    function getNodeByRelativeXPath(xpath){
        xpath=getRealXPath(_xpath_relative,xpath);
        return getNodeByXPath(xpath);
    }
    
    function appendNodeByXPath(xpath,node){
        var parent_node=getNodeByXPath(xpath);
        parent_node.appendChild(node);
        
        return parent_node;
    }
    
    function appendNodeByRootXPath(xpath,node){
        xpath=getRealXPath(_xpath_root,xpath);
        return appendNodeByXPath(xpath,node);
    }
    
    function appendNodeByRelativeXPath(xpath,node){
        xpath=getRealXPath(_xpath_relative,xpath);
        return appendNodeByXPath(xpath,node);
    }
    
    function appendNode2Root(node){
        var root_node=_xmlParam.documentElement;
        root_node.appendChild(node);
        
        return root_node;
    }
    
    function getDomByXPath(xpath){
        var oNodeList;
        oNodeList=_xmlParam.selectNodes(xpath);
        
        var new_dom=new XmlDom();
        var i;
        var count=oNodeList.length;
        for(i=0;i<count;i++){
            new_dom.appendNode2Root(oNodeList.item(i));
        }
        
        return new_dom;
    }
    
    function getDomByRootXPath(xpath){
        xpath=getRealXPath(_xpath_root,xpath);
        return getDomByXPath(xpath);
    }

    function getDomByRelativeXPath(xpath){
        xpath=getRealXPath(_xpath_relative,xpath);
        return getDomByXPath(xpath);
    }
    
    function removeAllChildNode(xpath){
        var tmp_xpath=xpath+"/*";
        var slt = _xmlParam.selectNodes(tmp_xpath);
        slt.removeAll();
    }
    
    function reset(){
        removeAllChildNode(_xpath_root);
    }

/***********************************************
modify
每次增加新的数据类型后，需要在这里补充代码

***********************************************/
    function toString(v){
        if(typeof(v)=="object"){
            if((v.constructor==Date)||(v.constructor==Datetime)){
                var year=v.getYear();
                var month=v.getMonth();
                var date=v.getDate();
                var hour=v.getHours();
                var minutes=v.getMinutes();
                var seconds=v.getSeconds();
                var milliseconds=v.getMilliseconds();

                v=year+"-"+month+"-"+date+" "+hour+":"+minutes+":"+seconds+"."+milliseconds;
            }
            else{

            }
        }
        else{
            if(typeof(v)=="boolean"){
                if(v){
                    v="true";
                }
                else{
                    v="false";
                }
            }
        }

        return v;
    }
}
