Javascript reads the Xml fields in the operation Sql

  • 2020-03-30 04:06:04
  • OfStack

In a project, we need to represent the information in the Xml fields in the database in a page, if we operate with Sql, read it. In this way, it is bound to lead to too complex, so I thought, if the Xml field read out, and then use Js to operate, so is not much easier, so the Internet to find some information. The method of JS reading Xml field information is implemented.

First we'll put a TextBox in the page for the Xml fields. Remember: you can't use Label, because the page will generate Js errors in case there is a "" in the Xml field information.

Next, that'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

And finally, I'm going to manipulate this 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; 
} 

}

Xml format:


<Info Remark=" Document template "><Common Remark=" General configuration "><DisCopy Remark=" Copy discount ">100</DisCopy><DisOriginal Remark=" discount ">100</DisOriginal><ArrearageLimit Remark=" Owe the ceiling ">0</ArrearageLimit><YearPrice Remark=" The annual fee, the yuan / years ">0</YearPrice></Common></Info>

Related articles: