客户端 使用XML DOM

  我们取出数据后可以以json的形式传到前端处理,也可以以Xml Dom的形式传到前端进行处理。下边例子是利用Jquery处理XML Dom的例子。

 

步骤:

  1、引用Jquery

  2、如果是IE,实例ActiveXObject;Firefox,实例DOMParser。

  3、处理

 

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " Default.aspx.cs "  Inherits = " XmlDom._Default "   %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head  runat ="server" >
    
< title ></ title >

    
< script  type ="text/javascript"  src ="js/jquery-1.4.2.js" ></ script >

    
< script  type ="text/javascript" >
        
// 得到XML Dom
         function  LoadXML(XmlString) {
            
var  xmlDoc;
            
// firefox等
             if  ( ! window.ActiveXObject) {
                
var  parser  =   new  DOMParser();
                xmlDoc 
=  parser.parseFromString(XmlString,  " text/xml " );
            } 
else  {
                
// IE
                xmlDoc  =   new  ActiveXObject( " Microsoft.XMLDOM " );
                xmlDoc.async 
=   " false " ;
                xmlDoc.loadXML(XmlString);
            }
            
return  xmlDoc;
        }
        
function  UseCllentXmlDom() {
            
try  {
                
var  string  =   " <Log><Content value='测试Xml Dom用法' /></Log> " ;
                
// 加载
                 var  xmlString  =  LoadXML(string);
                
var  xmlContent  =  $(xmlString).find( " Content " );
                
if  (xmlContent  !=   null ) {
                    $(xmlString).find(
" Content " ).each( function () {
                        
var  ContentValue  =  $( this ).attr( " value " );
                  //显示得到的数据
                        $(
" #DomValue " ).html(ContentValue);
                    });
                }
            }
            
catch  (e) {
                
throw  e;
            }
        }
    
</ script >

</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< style ="cursor: pointer"  onclick ="UseCllentXmlDom()" > 调用客户端XmlDom </ a >
        
< br  />
        显示数据:
        
< div  id ="DomValue" >
        
</ div >
    
</ div >
    
</ form >
</ body >
</ html >

 

 

在FireFox下,我测试时发现,将XML字符串直接给Jquery,Jquery也能直接处理;但在IE下不行。如下边代码。

代码
  < script  type ="text/javascript" >
        
// 仅在firefox下可行
         function  UseCllentXmlDom() {
            
try  {
                
var  string  =   " <Log><Content value='测试Xml Dom用法' /></Log> " ;
             //取XML 字符串
                xmlString 
=  $(string);
                
var  xmlContent  =  $(xmlString).find( " Content " );
                
if  (xmlContent  !=   null ) {
                    $(xmlString).find(
" Content " ).each( function () {
                        
var  ContentValue  =  $( this ).attr( " value " );
                  //显示得到的数据
                        $(
" #DomValue " ).html(ContentValue);
                    });
                }

            }
            
catch  (e) {
                
throw  e;
            }
        }
    
</ script >

 

 

 

 

 

你可能感兴趣的:(xml)