Javascript 操作 Sql中的Xml 欄位

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   java   

在一個項目當中,我們需要把資料庫中的Xml欄位中的資訊表現在頁面中,如果用Sql進行操作,讀取。這樣的話,必然導致太複雜,於是我想到了,如果把Xml欄位先讀出來,然後用Js去操作,這樣豈不是簡單了許多,於是網上尋找了一些資料。實現了JS讀Xml欄位資訊的方法。

 

首先我們在頁面中放一個TextBox 用來放Xml欄位。切記:不可用Label,因為萬一Xml欄位資訊中有“”這種符號時,頁面會產生Js錯誤。

 

接下去,就是重點了。付JS代碼:

[javascript] view plaincopyprint?
  1. function createXml(str){  
  2.   if(document.all){  
  3.     var xmlDom=new ActiveXObject("Microsoft.XMLDOM")  
  4.     xmlDom.loadXML(str)  
  5.     return xmlDom  
  6.   }  
  7.   else  
  8.     return new DOMParser().parseFromString(str, "text/xml")  
  9. }  
  10.   
  11. //以上的方法是將字串執行個體化為Xml  

 

 

最後就是操作這個Xml 了。。。

[javascript] view plaincopyprint?
  1. window.onload=function () {  
  2.    var str=document.getElementById("ctl00_ContentPlaceHolder1_TextBox1").value;  
  3.      var obj=createXml(str);  
  4.   
  5.         //得到根節點  
  6.         var root_node=obj.documentElement;  
  7.         
  8.    var yh1="";  
  9.         for (i=0;i< root_node.childNodes[0].childNodes.length-1;i++)  
  10.         {  
  11.             yh1+="   "+(i+1)+"、"+root_node.childNodes[0].childNodes[i].getAttribute("Remark")+":"+root_node.childNodes[0].childNodes[i].firstChild.nodeValue  ;  
  12.               
  13.               
  14.             yh1+="<br/>"  
  15.                         
  16.         }  
  17.  document.getElementById("ctl00_ContentPlaceHolder1_lblContent").innerHTML=yh1;  
  18.         }  
  19.          
  20. }  

 

付Xml格式:

[xhtml] view plaincopyprint?
  1. <Info Remark="檔案模板"><Common Remark="通用配置"><DisCopy Remark="複印件折扣">100</DisCopy><DisOriginal Remark="折扣">100</DisOriginal><ArrearageLimit Remark="欠費上限">0</ArrearageLimit><YearPrice Remark="年費,元/年">0</YearPrice></Common></Info>  

Javascript 操作 Sql中的Xml 欄位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.