JavaScript Read Xml_javascript Tips

Source: Internet
Author: User

Copy Code code as follows:
/**
* Get the collection object of XML file attributes
* @param xmldoc XML objects
* @param Name property names such as: User
* @return Return Array Object
* Example XML:
* <?xml version= ' 1.0 ' encoding= ' utf-8 '?>
* <ekuy>
* <user>
* <name>
* <cnname> Piglets </cnname>
* </name>
* <age>27</age>
* </user>
* <user>
* <name>
* <cnname> Xiao Xia </cnname>
* </name>
* <age>26</age>
* </user>
* </ekuy>
*/
function Getxmlarray (xmldoc, name) {
var keys = name.split ('. ');
var node = xmldoc.documentelement; Get root node
var Rtn = new Array ();
var n = 0;

for (var i=0; i<keys.length; i++) {
var childs = Node.childnodes; Get child node
var key = Keys[i];
for (var k=0; k<childs.length; k++) {
var child = Childs[k];
if (Child.nodename = = key) {//To determine whether the child nodes are compliant
if (i = = keys.length-1) {
Rtn[n] = child;
n++;
} else {
node = child;
Break
}
}
}
}


return RTN;
}


/**
* Get the value of the object from the Getxmlarray function
* @param node Nodes object
* @param name
* @return return String
*/
function getValue (node, name) {
var keys = name.split ('. ');

for (var i=0; i<keys.length; i++) {
var childs = Node.childnodes; Get child node
var key = Keys[i];
for (var k=0; k<childs.length; k++) {
var child = Childs[k];
if (Child.nodename = = key) {//To determine whether the child nodes are compliant
if (child.childNodes.length = = 1) {
If there is no byte point, the return value
return child.text;
} else {
and sub nodes, continue profiling
node = child;
Break
}
}
}
}


Return "";
}



Test:


var xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
Xmldoc.async = false;
Xmldoc.loadxml ("<?xml version=" 1.0 ' encoding= ' utf-8 '?><ekuy><user><name><cnname> ') piggy </cnname></name><age>27</age></user><user><name><cnname> Xiao Xia < /cnname></name><age>26</age></user><user><name><cnname> Small Chapter </ Cnname></name><age>25</age></user></ekuy> ");


var list = Getxmlarray (xmldoc, ' user ');
for (var i=0; i<list.length; i++) {
var obj = list[i];
document.write (GetValue (obj, ' name.cnname '));
document.write (GetValue (obj, ' age '));
document.write (' <br\/> ');
}

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.