Javascript 与 Xml

 Javascript中的基本DOM语法

//获取根节点
.documentElement
/*例:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.load("/abc.xml");
var xmlRoot = xmlDoc.documentElement;
*/

//查找唯一节点
getElementById("singleId")
/*例:
document.getElementById("txtName");
*/

//获取父元素
.parentElement
/*例:
window.alert(document.getElementById("txtName").parentElement.className);
*/

//获取子节点集
.childNodes
//获取指定位置节点
.childNodes[index]

//获取属性值
getAttribute("attributeName")

//查找唯一节点
selectSingleNode("nodeDir");
/*例:
var node = xmlDoc.selectSingleNode("/smil/body");
*/

 

<?xml version="1.0" encoding="utf-8" ?> 
<config>
    
<front>
        
<!--运营商类别(移动:mobile, 联通:unicom, 电信:telecom)-->
        
<frontItem name="boss">mobile</frontItem>

        
<!--主导航菜单-->
        
<frontItem name="mainMenu">
            
<![CDATA[
                <div class="nav_item" onClick="window.location.href='/';" onMouseOver="this.style.textDecoration='underLine';" onMouseOut="this.style.textDecoration='none';">首页</div>
                <div class="nav_item" onClick="window.location.href='/Sms/Normal.aspx';" onMouseOver="this.style.textDecoration='underLine';" onMouseOut="this.style.textDecoration='none';">短信</div>
                <div class="nav_item" onClick="window.location.href='/Mms/Default.aspx';" onMouseOver="this.style.textDecoration='underLine';" onMouseOut="this.style.textDecoration='none';">彩信</div>
                <div class="nav_item" onClick="window.location.href='/Sms/JoyfulNote.aspx';" onMouseOver="this.style.textDecoration='underLine';" onMouseOut="this.style.textDecoration='none';">快乐音符</div>
                <div class="nav_item" onClick="window.location.href='/Points.aspx';" onMouseOver="this.style.textDecoration='underLine';" onMouseOut="this.style.textDecoration='none';">积分</div>
                <div class="nav_item" onClick="window.location.href='/Award.aspx';" onMouseOver="this.style.textDecoration='underLine';" onMouseOut="this.style.textDecoration='none';">大奖</div>
                <div class="nav_item" onClick="window.location.href='/Help/Help.html';" onMouseOver="this.style.textDecoration='underLine';" onMouseOut="this.style.textDecoration='none';">帮助中心</div>
            
]]>
        
</frontItem>

        
<!--是否显示首页搜索模块-->
        
<frontItem name="searchEnabled">1</frontItem>
        
<!--搜索标签-->
        
<frontItem name="searchTag">
            
<![CDATA[
                爱情,端午节,搞笑,彩信,音乐
            
]]>
        
</frontItem>
        
<!--搜索类别-->
        
<frontItem name="searchType">
            
<![CDATA[
                全部,彩信,短信,图形短信,快乐音符
            
]]>
        
</frontItem>

        
<!--是否显示首页通知公告模块-->
        
<frontItem name="bulletinEnabled">1</frontItem>
        
<!--首页通知公告模块显示数量-->
        
<frontItem name="bulletinNumber">4</frontItem>

        
<!--是否显示首页积分排名模块-->
        
<frontItem name="rankEnabled">1</frontItem>

        
<!--是否显示首页上月获奖名单模块-->
        
<frontItem name="winnerEnabled">1</frontItem>

        
<!--是否显示新手上路(帮助)模块-->
        
<frontItem name="helpEnabled">1</frontItem>
        
<!--首页新手上路部分-->
        
<frontItem name="novice">
            
<![CDATA[
                <a href='#' target='_blank'>· 我是免费客户,为什么一段时间后收不到趣味短信?</a>,
                <a href='#' target='_blank'>· 套餐客户参与活动会另外按条收费吗?</a>,
                <a href='#' target='_blank'>· 成为点数会员是包月的,我转发短信免费吗?</a>,
                <a href='#' target='_blank'>· 登记短信时提示已经受理,但通不过审核?</a>,
                <a href='#' target='_blank'>· 登记短信时提示已经受理,为什么通不过审核?</a>
            
]]>
        
</frontItem>

        
<!--是否显示首页彩信模块-->
        
<frontItem name="mmsEnabled">1</frontItem>
        
<!--首页彩信模块显示数量-->
        
<frontItem name="mmsNumber">8</frontItem>

        
<!--是否显示首页短信模块-->
        
<frontItem name="smsEnabled">1</frontItem>
        
<!--首页短信模块显示数量-->
        
<frontItem name="smsNumber">7</frontItem>

        
<!--是否显示首页图型短信模块-->
        
<frontItem name="graphEnabled">1</frontItem>
        
<!--首页图型短信模块显示数量-->
        
<frontItem name="graphNumber">4</frontItem>

        
<!--是否显示首页快乐音符模块-->
        
<frontItem name="musicEnabled">1</frontItem>
        
<!--首页快乐音符模块显示数量-->
        
<frontItem name="musicNumber">7</frontItem>

        
<!--版权部分-->
        
<frontItem name="copyRight">
            
<![CDATA[
                中国联通有限公司福建分公司版权所有<br />闽ICP备05009249号
            
]]>
        
</frontItem>

        
<frontItem name="crcMessage">
            
<![CDATA[
                您在分享A计划中注册,验证码为:{0}
            
]]>
        
</frontItem>

        
<!--PIM短信收藏中自定义分类限制数量-->
        
<frontItem name="favTagNum">5</frontItem>

        
<!--PIM通讯录中自定义分类限制数量-->
        
<frontItem name="addressTagNum">5</frontItem>

    
</front>
</config>

 

//根据节点名称获取节点值
function getXmlValue(itemName)
{
    
//判断浏览器是否支持ActiveX控件
    if(window.ActiveXObject)
    {
        
var result = "";
        
//获得操作的xml文件的对象 
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        
//指定XML文件的下载是否与XML的处理异步进行
        xmlDoc.async = false;
        xmlDoc.load(
"/Configuration/Config.xml"); 
        
if(xmlDoc != null)
        {
            
//解析XML是否出错,可通过 xmlDoc.parseError.reason 来捕获错误信息
            if(xmlDoc.parseError.errorCode == 0)
            {
                
//获得根接点 
                var root = xmlDoc.documentElement;
                
//遍历根节点
                for(var i = 0; i < root.childNodes.length; i++)
                {
                    
//找到front节点
                    if(root.childNodes[i].nodeName == "front")
                    {
                        
//遍历front节点
                        for(var j = 0; j < root.childNodes[i].childNodes.length; j++)
                        {
                            
//查找目标节点值
                            if(root.childNodes[i].childNodes[j].nodeName == "frontItem")
                            {
                                
if(root.childNodes[i].childNodes[j].getAttribute("name"== "boss")
                                {
                                    result 
= root.childNodes[i].childNodes[j].text;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
return result;
}

用DOM方式动态添加和删除DIV
/*
今天写一个超复杂的用JS多维动态构造页面的功能,发现动态构造控件时如果用innerHTML的方式,控件的值会丢失,但如果用DOM方式,就能很好解决该问题,简单示范代码如下:
*/
//添加div
var _parent = document.getElementById("parent");
var _son = document.createElement("div");
_son.className 
= "my_class";
_son.id 
= "my_id";
_son.innerHTML 
= "some string";
_parent.appendChild(_son);
//也可插入指定位置
_parent.insertBefore(_son, _parent.childNodes[0]);

//删除div
var div = document.getElementById("my_id");
div.removeNode(
true);

posted on 2007-10-17 13:32  吴剑  阅读(423)  评论(0编辑  收藏  举报

导航