Javascript reads xml

Source: Internet
Author: User

Copy codeThe Code is as follows :/**
* Get the set object of the XML file attributes.
* @ Param xmlDoc XML Object
* @ Param name attribute name, for example, user
* @ Return returns an Array object.
* Example XML:
* <? Xml version = '1. 0' encoding = 'utf-8'?>
* <Ekuy>
* <User>
* <Name>
* <Cnname> pig </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.doc umentElement; // get the root node
Var rtn = new Array ();
Var n = 0;

For (var I = 0; I <keys. length; I ++ ){
Var childs = node. childNodes; // obtain the subnode
Var key = keys [I];
For (var k = 0; k <childs. length; k ++ ){
Var child = childs [k];
If (child. nodeName = key) {// determines whether the child node matches
If (I = keys. length-1 ){
Rtn [n] = child;
N ++;
} Else {
Node = child;
Break;
}
}
}
}


Return rtn;
}


/**
* Get the value in the object obtained by the getXMLArray function.
* @ Param node object
* @ Param name
* @ Return returns String
*/
Function getValue (node, name ){
Var keys = name. split ('.');

For (var I = 0; I <keys. length; I ++ ){
Var childs = node. childNodes; // obtain the subnode
Var key = keys [I];
For (var k = 0; k <childs. length; k ++ ){
Var child = childs [k];
If (child. nodeName = key) {// determines whether the child node matches
If (child. childNodes. length = 1 ){
// Return value if there is no byte point
Return child. text;
} Else {
// There are subnodes to continue the analysis
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> Piglet </cnname> </name> <age> 27 </age> </user> <name> <cnname> Xiao Xia </cnname> </name> <age> 26 </age> </user> <name> <cnname> 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 \/> ');
}

Related Article

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.