In a project, we need to display the information in the Xml field in the database on the page, and read it if we use Sql for operation. In this case, it will inevitably lead to too complicated. So I thought that if I read out the Xml field first and then operate it with Js, wouldn't it be much simpler? So I searched for some information online. Implements the method of reading Xml field information by JS.
First, we put a TextBox in the page to put the Xml field. Remember: Label is not available, because if there is a symbol like "" in the Xml field information, the page will generate a Js error.
Next, it's the 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
Finally, it's Xml
window.onload=function () { var str=document.getElementById("ctl00_ContentPlaceHolder1_TextBox1").value; var obj=createXml(str); //Get the root node 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 Xml format:
<Info Remark="File Templates"><Common Remark="General Configuration"><DisCopy Remark="Copy Discount">100</DisCopy><DisOriginal Remark="Discount">100</DisOriginal><ArrearageLimit Remark="Upload of arrears">0</ArrearageLimit><YearPrice Remark="Annual Fee, Yuan/Year">0</YearPrice></Common></Info>