Xml/jsdom processing

Source: Internet
Author: User

The XML file Login.xml is 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= "value hit" value= "4" ></P>
</EconomyProperty>
</Login>
Now I need to manipulate the contents of this XML file.
First, we need to load this XML file, JS loaded XML file, is through XMLDOM.
Loading an 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 is out, and then I'm going to work on this document.
For example, we now need to get the properties of node login/weapon/w's first node, so we can do this.


First, the XML object is judged
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, the recommended use of IE5.0 above can solve this problem! ');
window.location.href= '/index.aspx ';
}

return xmldoc;
}

Then start getting the property values for 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 such, of course, my writing in the program has been applied to the actual. Give it together 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<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).
Also available through XmlDoc.documentElement.childNodes (1). ChildNodes (0). getattribute (' Text ') for access.
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 the XML format: &LT;A&GT;B&LT;/B&GT;, so you can get the B value.
XmlDoc.documentElement.childNodes (0). Haschild, can determine if there are child nodes

In my experience, it is best to use the getElementsByTagName (XPath) method to access the nodes, because it can be used to locate nodes directly through XPath, which will have better performance.

Source: Http://blog.163.com/[email protected]/blog/static/445360592008111653020759/

Xml/jsdom processing

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.