Home  >  Article  >  Web Front-end  >  Javascript reads Xml fields in Sql_javascript skills

Javascript reads Xml fields in Sql_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:34:261322browse

In a project, we need to display the information in the Xml field in the database on the page. If we use Sql to operate and read. In this case, it will inevitably lead to too much complexity, so I thought that it would be much simpler if I read the Xml field first and then use JS to operate it, so I searched for some information on the Internet. Implemented JS method to read Xml field information.

First we put a TextBox in the page to hold the Xml field. Remember: Label cannot be used, because if there is a symbol like "" in the Xml field information, the page will generate a Js error.

The next step is the key point. Pay JS code:

function createXml(str){ 
  if(document.all){ 
    var xmlDom=new ActiveXObject("Microsoft.XMLDOM") 
    xmlDom.loadXML(str) 
    return xmlDom 
  } 
  else 
    return new DOMParser().parseFromString(str, "text/xml") 
} 

//The above method is to instantiate the string into Xml

The last step is to operate this Xml

window.onload=function () { 
var str=document.getElementById("ctl00_ContentPlaceHolder1_TextBox1").value; 
var obj=createXml(str); 

//得到根节点 
var root_node=obj.documentElement; 

var yh1=""; 
for (i=0;i< root_node.childNodes[0].childNodes.length-1;i++) 
{ 
yh1+=" "+(i+1)+"、"+root_node.childNodes[0].childNodes[i].getAttribute("Remark")+":"+root_node.childNodes[0].childNodes[i].firstChild.nodeValue ; 


yh1+="<br/>" 

} 
document.getElementById("ctl00_ContentPlaceHolder1_lblContent").innerHTML=yh1; 
} 

}

Pay in Xml format:

<Info Remark="文件模板"><Common Remark="通用配置"><DisCopy Remark="复印件折扣">100</DisCopy><DisOriginal Remark="折扣">100</DisOriginal><ArrearageLimit Remark="欠费上限">0</ArrearageLimit><YearPrice Remark="年费,元/年">0</YearPrice></Common></Info>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn