Tree lists are used in a recent project. mztree is used, but xml is used for data. xml is a tree directory, So we plan to directly Parse xml files using JavaScript, I have found some JavaScript methods for operating xml on the Internet. Here I will make a summary.
My xml file Login. xml is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Login> <Character> <C Text = "" Value = "0"> </C> <C Text = "" Value = "1"> </C> <C Text = "passion" Value = "2"> </C> <C Text = "cool" Value = "3"> </C> <C Text =" cold "Value =" 4 "> </C> </Character> <Weapon> <W Text =" "Value =" 0 "> </W> <W Text = "beam configuration knife" Value = "1"> </W> </Weapon> <economy property> <P Text = "average" Value = "0"> </P> <P Text = "attach importance to attacks" Value = "1"> </P> <P Text = "attach importance to agility" Value = "2"> </P> <P Text = ""Pay attention to defense" Value = "3"> </P> <P Text = "Pay attention to hit" Value = "4"> </P> </economical myproperty> </Login>
Now I need to operate on the content of this xml file.
First, we need to load this xml file. xml files are loaded in JavaScript through XMLDOM:
// Load the xml document
LoadXML = function (xmlFile)
{
Var xmlDoc;
If (window. ActiveXObject)
{
XmlDoc = new ActiveXObject ('Microsoft. XMLDOM ');
XmlDoc. async = false;
XmlDoc. load (xmlFile );
}
Else if (document. implementation & document. implementation. createDocument)
{
XmlDoc = document. implementation. createDocument ('','', null );
XmlDoc. load (xmlFile );
}
Else
{
Return null;
}
Return xmlDoc;
}
The xml file object has been released. Next, I will operate on this document.
For example, if we need to get the attributes of the first node of the node Login/Weapon/W, we can do the following:
// First, judge the xml Object
CheckXMLDocObj = function (xmlFile)
{
Var xmlDoc = loadXML (xmlFile );
If (xmlDoc = null)
{
Alert ('your browser does not support reading xml files, so this page prohibits your operations. We recommend you use IE5.0 or above to solve this problem! ');
Window. location. href = '/Index. aspx ';
}
Return xmlDoc;
}
// Then obtain the 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 ')
However, the way I write in my program is like this. Of course, the way I write in the program has been applied to the actual application.
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 <l; I ++)
{
Var option = document. createElement ('option ');
Option. value = n [I]. getAttribute ('value ');
Option. innerHTML = n [I]. getAttribute ('text ');
E. appendChild (option );
}
}
}
In the above access code, we use xmlDoc. getElementsByTagName (xPath.
You can also use xmldoc.doc umentElement. childNodes (1)... childNodes (0). getAttribute ('text') for access.
Some common methods:
◆ XmlDoc.doc umentElement. childNodes (0). nodeName, you can get the name of this node;
◆ XmlDoc.doc umentElement. childNodes (0 ). nodeValue to obtain the value of this node. the value is in xml format like this: <a> B </B>, so you can get the value of B;
◆ XmlDoc.doc umentElement. childNodes (0). hasChild, you can determine whether a subnode exists.
In my experience, it is best to use the getElementsByTagName (xPath) method to access the node, because it can be used to locate the node directly through xPath, which will provide better performance.
- How to use C # and XML to create a dynamic hierarchical menu
- Processing XML service definitions through Java programming
- Use the Treeview control and XML in ASP. NET