创建xmldocument对象,并读取xml,绑定到select, ie/ff通用

 dom对象可以方便的访问xml数据源,以下简单的演示了此功能:

<NewDataSet>
  <Table>
    <id>1</id>
    <sheng_num>10</sheng_num>
    <sheng_name>安徽</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>2</id>
    <sheng_num>11</sheng_num>
    <sheng_name>北京</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>3</id>
    <sheng_num>12</sheng_num>
    <sheng_name>福建</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>4</id>
    <sheng_num>13</sheng_num>
    <sheng_name>甘肃</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>5</id>
    <sheng_num>14</sheng_num>
    <sheng_name>广东</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>6</id>
    <sheng_num>15</sheng_num>
    <sheng_name>广西</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>7</id>
    <sheng_num>16</sheng_num>
    <sheng_name>贵州</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>8</id>
    <sheng_num>17</sheng_num>
    <sheng_name>海南</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>9</id>
    <sheng_num>18</sheng_num>
    <sheng_name>河北</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>10</id>
    <sheng_num>19</sheng_num>
    <sheng_name>河南</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>11</id>
    <sheng_num>20</sheng_num>
    <sheng_name>黑龙江</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>12</id>
    <sheng_num>21</sheng_num>
    <sheng_name>湖北</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>13</id>
    <sheng_num>22</sheng_num>
    <sheng_name>湖南</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>14</id>
    <sheng_num>23</sheng_num>
    <sheng_name>吉林</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>15</id>
    <sheng_num>24</sheng_num>
    <sheng_name>江苏</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>16</id>
    <sheng_num>25</sheng_num>
    <sheng_name>江西</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>17</id>
    <sheng_num>26</sheng_num>
    <sheng_name>辽宁</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>18</id>
    <sheng_num>27</sheng_num>
    <sheng_name>内蒙古</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>19</id>
    <sheng_num>28</sheng_num>
    <sheng_name>宁夏</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>20</id>
    <sheng_num>29</sheng_num>
    <sheng_name>青海</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>21</id>
    <sheng_num>30</sheng_num>
    <sheng_name>山东</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>22</id>
    <sheng_num>31</sheng_num>
    <sheng_name>山西</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>23</id>
    <sheng_num>32</sheng_num>
    <sheng_name>陕西</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>24</id>
    <sheng_num>33</sheng_num>
    <sheng_name>上海</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>25</id>
    <sheng_num>34</sheng_num>
    <sheng_name>四川</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>26</id>
    <sheng_num>35</sheng_num>
    <sheng_name>天津</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>27</id>
    <sheng_num>36</sheng_num>
    <sheng_name>新疆</sheng_name>
    <sheng_Flag>0</sheng_Flag>
  </Table>
  <Table>
    <id>28</id>
    <sheng_num>37</sheng_num>
    <sheng_name>西藏</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>29</id>
    <sheng_num>38</sheng_num>
    <sheng_name>云南</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>30</id>
    <sheng_num>39</sheng_num>
    <sheng_name>浙江</sheng_name>
    <sheng_Flag>1</sheng_Flag>
  </Table>
  <Table>
    <id>31</id>
    <sheng_num>40</sheng_num>
    <sheng_name>重庆</sheng_name>
    <sheng_Flag>0</sheng_Flag>
  </Table>
  <Table>
    <id>32</id>
    <sheng_num>41</sheng_num>
    <sheng_name>香港</sheng_name>
    <sheng_Flag>0</sheng_Flag>
  </Table>
  <Table>
    <id>33</id>
    <sheng_num>42</sheng_num>
    <sheng_name>澳门</sheng_name>
    <sheng_Flag>0</sheng_Flag>
  </Table>
  <Table>
    <id>34</id>
    <sheng_num>43</sheng_num>
    <sheng_name>台湾</sheng_name>
    <sheng_Flag>0</sheng_Flag>
  </Table>
  <Table>
    <id>35</id>
    <sheng_num>44</sheng_num>
    <sheng_name>国外</sheng_name>
    <sheng_Flag>0</sheng_Flag>
  </Table>
</NewDataSet>

   <!--创建dom对象,ie/ff通用 开始-->
    function   createXmlDom(xmlstr){                                                                             
        if(window.DOMParser)//firefox内核的浏览器                                                                
        {                                                                                                        
            var   p   =   new   DOMParser();                                                                     
            return   p.parseFromString(   xmlstr,   "text/xml"   );                                              
        }                                                                                                        
        else   if(   window.ActiveXObject   )//ie内核的浏览器                                                    
        {                                                                                                        
            var   doc   =   new   ActiveXObject(   "Msxml2.DOMDocument"   );                                     
            doc.loadXML(xmlstr);                                                                                 
            return   doc;                                                                                        
        }                                                                                                        
        else                                                                                                     
           return   false;                                                                                      
    }                                                                                                            
 <!--创建dom对象,ie/ff通用 结束-->

<!--读取xml同步获取获取省份 开始-->
    function loadprovincexml()                                                                                   
    {                                                                                                            
        var myajax = new ajaxsz();                                                                               
        var province =  myajax.sendData("POST","http://localhost:5964/GetData/GetXML.aspx",false,"");            
        province = province.replace(//n/g,"").replace(//s+/g,"");//替换换行和空格,ff的换行和空格都算一个节点                
                                                                                                                  
        var select1 = document.getElementById("Select1");                                                        
      for (i = select1.length; i >= 0; i--)                                                                    
      {                                                                                                        
              select1.remove(i);                                                                                 
        }                                                                                                        
                                                                                                                 
        select1.options.add(new   Option("---选择省份---","0"));                                                 
                                                                                                                 
      var xmldoc = new createXmlDom(province);                                                                 
        var nodes=xmldoc.documentElement.childNodes;                                                             
     for(var i=0;i<nodes.length;i++)                                                                          
     {                                                                                                        
         var newOption = document.createElement("OPTION");                                                    
            newOption.text=nodes[i].childNodes[2].childNodes[0].nodeValue;                                       
            newOption.value=nodes[i].childNodes[1].childNodes[0].nodeValue;                                      
            select1.options.add(newOption);                                                                      
     }                                                                                                        
     }
<!--读取xml同步获取获取省份 结束-->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值