﻿//*************** XML ***********************
//作者：王建川 ()
//网址：http://www.ininnet.com
//版本：v1.4
//Q　Q：4513427 е玄峰
//免费使用，请保留作者信息

var IninNet_RegExp_Js = 
{
  Version: '1.4.5'
}

// *****************************************组织xml数据结构
// 使用方法
/*
1、
var xml = new IninNet_Xml();
xml.add(new xml.childNode("节点名称","值"),new xml.childNode("节点名称","值"));
2、
var xml = new IninNet_Xml();
xml.add(
    new xml.create(xml,"节点名称").create(
        new xml.childNode("节点名称","值"),
        new xml.childNode("节点名称","值")
    ),
    new xml.create(xml,"节点名称").create(
        new xml.childNode("节点名称","值"),
        new xml.childNode("节点名称","值")
    ),
    new xml.childNode("节点名称","值")
);

调用方法1：
xml.update();//组织结构 && 返回是否正确
xml.xml //取XML数据结构 必须运行update()
调用方法2：
xml.getXml();
调用方法3：
xml.LoadXML();加载到ActiveXObject (XML)对象里
下面你就可以调用xml.xmlDom的XML对象了
*/

var IninNet_Xml = function(id){
/// <summary>创建XML结构文件</summary>
    if(id != null)
        eval("window." + id +"=this");
    xml=null;//字符串的xml内容
    xmlDom=null;//XML对象 ActiveXObject
    if(arguments.length >0)
        this.root = arguments[0];
    else
        this.root = "root";
    this.childNodes = new Array();
}

IninNet_Xml.prototype.getXml = function(){
/// <summary>获取XML内容</summary>
    this.update();
    return this.xml;
}
IninNet_Xml.prototype.add = function(){
/// <summary>添加结构</summary>
    for(var i=0;i<arguments.length;i++)
    {
        if(arguments[i] == null)continue;
        if(arguments[i].name==null || arguments[i].value == null)
            return false;
        this.childNodes[this.childNodes.length] = arguments[i];
    }
    return this.update();
}
IninNet_Xml.prototype.addItem = function(name,value,cdata){
/// <summary>单独添加节点</summary>
/// <param name="name">节点名称</param>
/// <param name="value">节点值</param>
/// <param name="cdata">是否设为内容型CDATA 默认为:Flase</param>
    this.add(new this.childNode(name,value,cdata));
}
IninNet_Xml.prototype.update = function(){
/// <summary>更新结构，以便进行获取，返加true则表示结构正确</summary>
    var _tempXml = "";
    for(var i=0;i<this.childNodes.length;i++)
    {
        if(this.childNodes[i].constructor == String)
        {
            _tempXml += "<" + this.childNodes[i] + ">";
        }else{
            if(this.childNodes[i].name == null || this.childNodes[i].name == "")
            {
                alert("节点名称不能为空");
                return false;
            }
            if(this.childNodes[i].cdata == null || this.childNodes[i].cdata == true)
                _tempXml += "<" + this.childNodes[i].name + "><![CDATA[" + (this.childNodes[i].value == null ? "" : this.childNodes[i].value) + "]]></" + this.childNodes[i].name + ">"; 
            else
                _tempXml += "<" + this.childNodes[i].name + ">" + (this.childNodes[i].value == null ? "" : this.childNodes[i].value) + "</" + this.childNodes[i].name + ">"; 
        }
    }
    if(_tempXml != ""){
        this.xml = "<" + this.root + ">" + _tempXml + "</" + this.root + ">";
        return true;
    }
    else
        return false;
}
IninNet_Xml.prototype.LoadXML = function()
{
    if(this.update())
    {
        if (window.ActiveXObject){
            this.xmlDom = new ActiveXObject("Microsoft.XMLDOM");
        }else{
            if (document.implementation && document.implementation.createDocument)
                this.xmlDom=document.implementation.createDocument("text/xml","doc",null);
        }
        this.xmlDom.async = false;
        this.xmlDom.preserveWhiteSpace = true;
        if(arguments.length == 0)
            return this.xmlDom.loadXML(this.xml);
        else
            return this.xmlDom.loadXML(arguments[0]);
    }else{
        return null;
    }
}
IninNet_Xml.prototype.create = function(x,childName)
{
    if(x.constructor == null)
    {
        alert("必须指定IninNet_Xml原型\nIninNet_Xml.prototype.create(原型,\"节点名称\")");
        return false;
    }
    childName = (childName == null) ? "item" : childName;
    this.xml = x;
    this.xml.childNodes[this.xml.childNodes.length] = childName;
    this.item = childName;
    this.create = function()
    {
        for(var i=0;i<arguments.length;i++)
        {
            if(arguments[i] != null)
            {
                this.xml.childNodes[this.xml.childNodes.length] = arguments[i];
            }else{
                if(arguments[i].name==null)return false;
                this.xml.childNodes[this.xml.childNodes.length] = arguments[i];
            }
        }
        this.xml.childNodes[this.xml.childNodes.length] = "/" + this.item;
    }
}

IninNet_Xml.prototype.childNode = function(name,value,cdata){
/// <summary>创建节点</summary>
/// <param name="name">节点名称</param>
/// <param name="value">节点值</param>
/// <param name="cdata">是否设为内容型CDATA 默认为:Flase</param>
    this.name = name;
    this.value = value;
    this.cdata = cdata;
}