xml file Login.xml is as follows.
;
< > Character>
">
< P Text="Pay attention to defense" Value="3">
;
Now we need to operate the content of this xml file.
First, we need to load this xml file. The xml file is loaded in js through XMLDOM To proceed.
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false; ation.createDocument('', ' ', null);
xmlDoc.load(xmlFile);
}
else
{
return null;
}
}
The xml file object comes out, and next I will operate on this document.
For example, we now need to get the attributes of the first node of the node Login/Weapon/W, then we You can proceed as follows.
Copy the code
The code is as follows:
// First judge the xml object
checkXMLDocObj = function(xmlFile)
{
var xmlDoc = loadXML(xmlFile);
if(xmlDoc==null)
{
alert('Your browser does not support xml file reading, so this page prohibits your operation. It is recommended to use IE5.0 or above to solve this problem!');
window.location.href= '/Index.aspx';
}
return xmlDoc;
}
// Then start to get the required attribute value of the first node of Login/Weapon/W
var xmlDoc = checkXMLDocObj('/EBS/XML/Login.xml');
var v = xmlDoc.getElementsByTagName('Login/Weapon/W')[0].childNodes.getAttribute('Text')
The way I write it in my program is like this. Of course, the way I write it in the program has been applied in practice. It is also given for viewing
initializeSelect = function(oid, xPath)
{
var xmlDoc = checkXMLDocObj('/EBS/XML/Login.xml');
var n;
var l;
var e = $(oid);
if(e!=null)
{
n = xmlDoc.getElementsByTagName(xPath)[0].childNodes;
l = n.length;
for(var i=0; i {
var option = document.createElement('option');
option.value = n[i].getAttribute('Value');
option.innerHTML = n[i].get Attribute('Text ');
e.appendChild(option);
carried out.
It can also be accessed through xmlDoc.documentElement.childNodes(1)..childNodes(0).getAttribute('Text').
Some common methods:
xmlDoc.documentElement.childNodes(0 ).nodeName, you can get the name of this node.
xmlDoc.documentElement.childNodes(0).nodeValue, you can get the value of this node. This value comes from the xml format like this:
b b>, so you can get the value b.
xmlDoc.documentElement.childNodes(0).hasChild, you can determine whether there are child nodes
According to my experience, it is best to use getElementsByTagName(xPath) method to access the node, because this way the node can be located directly through xPath, which will have better performance.