This article Article This is about operating XML files in Js.
My XML file login. XML is as follows. <? XML version = "1.0" encoding = "UTF-8" ?>
< Login >
< Character >
< C Text = "Hot blood" Value = "0" > </ C >
< C Text = "Weak gas" Value = "1" > </ C >
< C Text = "Passion" Value = "2" > </ C >
< C Text = "Calm" Value = "3" > </ C >
< C Text = "Cold" Value = "4" > </ C >
</ Character >
< Weapon >
< W Text = "Beam sword" 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 = "Agile" Value = "2" > </ P >
< P Text = "Pay attention to defense" Value = "3" > </ P >
< P Text = "Pay attention to hit" Value = "4" > </ P >
</ Economy Property >
</ 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 JS through xmldom. // Load 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 comes out. 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 proceed as follows.
// First, judge the XML Object
Checkxmldocobj = Function (Xmlfile)
{
VaR Xmldoc = Loadxml (xmlfile );
If (Xmldoc = Null )
{
Alert ('the your browser does not support reading XML files, so this page prohibits your operations. We recommend that you use ie5.0 or above to solve this problem. ! ');
Window. Location. href = ' / Index. aspx ';
}
Return Xmldoc;
}
// Then obtain the attribute values 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 ')
While my Program Of course, the method 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 );
}
}
}
AccessCodeIn, we use xmldoc. getelementsbytagname (XPath.
You can also access it through xmldoc.doc umentelement. childnodes (1)... childnodes (0). getattribute ('text.
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 we can get the value of B.
Xmldoc.doc umentelement. childnodes (0). haschild, which can be used to 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.
Posted on Ma Zhiyuan reading (19) Comments (1) EDIT favorites reference favorites to 365key category: My growth history