The initial idea was to register to determine if the user name to register was duplicated.
Parameters:
Str_xmlurl: The address of a remote XML, such as Http://192.168.1.19/test/xml.xml
Str_dirpath: The path of the node to look for, such as xmldocument/test[0]/newname2/childs[2]
JS Code:
The following is a reference fragment: <script type= "Text/javascript" > function Getxmlnode (Str_xmlurl, Str_dirpath) { if (!str_xmlurl | |!str_dirpath) return false;
var _bool_ie = (window. ActiveXObject? True:false);//Get browser type
Initializing the XmlDocument object var _obj_xmldoc; if (_bool_ie) _obj_xmldoc = new ActiveXObject (' Microsoft.XMLDOM '); Else _obj_xmldoc = Document.implementation.createDocument ("", "", null);
/* Set Asynchronous processing This function does not require any action to be performed before the XML file is read. Therefore, the asynchronous processing function is turned off. */ _obj_xmldoc.async = false; Reading XML file contents Try { _obj_xmldoc.load (Str_xmlurl); }//try catch (E) { Read failed return false; }//catch /* Parse XML file content according to the path * * /*mozila firefox*/ if (!_bool_ie) { /* Parse the DOM path and verify Requires the path to use '/' as the separator Index of child nodes can be specified in the path using ' [n] ' such as "xmldocument/item[1]" represents the second item node under the first XmlDocument node under the root node If you do not use this method to default to the first node */ var _arr_dirpath = str_dirpath.split ('/'); if (!_arr_dirpath[0]) _arr_dirpath.shift (); if (!_arr_dirpath) return false;
var _obj_aimelement = _obj_xmldoc; Defines the regular expression object used to get the node index var _obj_reg =/\[([0-9]+) \]/ig; /* Get node based on DOM Path Resolution * * for (var _int_i = 0; _int_i < _arr_dirpath.length; _int_i + +) { Currently qualified node index var _int_localindex = 0; The node index specified in the DOM path var _int_aimindex = 0; Gets the relative path of the node that is currently being resolved var _str_dirpath = _arr_dirpath[_int_i]; Gets the index of the node currently being resolved var _arr_result = _obj_reg.exec (_str_dirpath); Search for identity, if the identity is false it indicates that the specified node is not obtained var bool_catch = false; Organize node relative path and node index if node index number is specified if (_arr_result) { _int_aimindex = _arr_result[1]; _str_dirpath = _str_dirpath.replace (_arr_result[0], ""); }//if
Gets all child nodes of the current node var _arr_childnodes = _obj_aimelement.childnodes; Queries the node that matches the "relative path" and the node index number of the node currently being resolved in the child nodes of the current node for (var _int_j = 0; _int_j < _arr_childnodes.length; _int_j + +) { child node and path matching if (_arr_childnodes[_int_j].nodename = = _str_dirpath) { If the index matches the path, the node is obtained Otherwise, the current qualifying node index is added "1" if (_int_localindex = = _int_aimindex) { _obj_aimelement = _arr_childnodes[_int_j]; Bool_catch = true; Break }//if Else _int_localindex + 1; }//if }//for Returns an error if the specified node is not obtained if (!bool_catch) { return false; } }//for Returns the contents of a searched node return (_obj_aimelement.childnodes[0].nodevalue); }//if
/*microsoft ie*/ Try { Returns the contents of a searched node Return _obj_xmldoc.selectnodes (Str_dirpath) [0].text; } catch (E) { return false; } return false; }//function Getxmlnode () Alert (Getxmlnode (' Http://192.168.1.19/test/xml.xml ', ' xmldocument/test[0]/newname2/childs[2] '); </script> |
XML code:
The following is a reference fragment: <?xml version= "1.0" encoding= "Utf-8"?> <XMLDocument> <test> <newName> <name>name</name> <childs>test000</childs> <childs>test998</childs> <childs>test997</childs> <childs> <childs>test889</childs> <childs>test888</childs> </childs> </newName> <newName2> <childs>test996</childs> <childs>test995</childs> <childs>test994</childs> </newName2> </test> <newName3> <childs>test993</childs> <childs>test992</childs> <childs>test991</childs> </newName3> <STR_WEB_HOST>http://localhost:8080/WebTemp/</STR_WEB_HOST> <STR_SYS_CHARSET>utf-8</STR_SYS_CHARSET> </XMLDocument> |