Javascript+xml operation

Source: Internet
Author: User
Tags object return window xpath access
Xml

My XML file is login.xml as follows.
<?xml version= "1.0" encoding= "Utf-8"?>
<Login>
<Character>
<c text= "Blood" value= "0" ></C>
<c text= "Weak gas" 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= "Beam sword" value= "0" ></W>
<w text= "beam matching knife" value= "1" ></W>
</Weapon>
<EconomyProperty>
<p text= "average type" value= "0" ></P>
<p text= "Value Attack" value= "1" ></P>
<p text= "Focus on Agility" value= "2" ></P>
<p text= "Focus on Defense" value= "3" ></P>
<p text= "focus on Hit" value= "4" ></P>
</EconomyProperty>
</Login>
Now I need to manipulate the contents of this XML file.
First of all, we need to load this XML file, JS load XML file, is through the 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, and then I'm going to work on this document.
For example, we now need to get the properties of the first node login/weapon/w node, so we can do the following.


First, the XML object is judged
Checkxmldocobj = function (xmlFile)
{
var xmldoc = Loadxml (xmlFile);
if (xmldoc==null)
{
Alert (' Your browser does not support XML file reads, so this page prohibits your operation, the recommended use of IE5.0 above can solve this problem! ');
window.location.href= '/index.aspx ';
}

return xmldoc;
}

Then start to get the property value of the first node of the desired login/weapon/w
var xmldoc = checkxmldocobj ('/ebs/xml/login.xml ');
var v = xmldoc.getelementsbytagname (' login/weapon/w ') [0].childnodes.getattribute (' Text ')
And I write in my program is like this, of course, I am in the process of writing is already applied to the actual. and give out to see
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 access code above, we do this through Xmldoc.getelementsbytagname (XPath).
You can also pass XmlDoc.documentElement.childNodes (1). ChildNodes (0). getattribute (' Text ') is accessed.
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 is derived from this XML format: &LT;A&GT;B&LT;/B&GT, so you can get the value of B.
XmlDoc.documentElement.childNodes (0). Haschild, you can determine whether there are child nodes

In my experience, it is best to use the getElementsByTagName (XPath) approach to access the nodes, as this can be done directly through XPath to locate the node, which would have better performance.

Http://www.cnblogs.com/skylaugh/archive/2006/09/15/505393.html



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.