Fire Station Building Institute (Bkjia.com) Technical DocumentationThis article mainly introduces an example that is compatible with firefoxfirefox's JS to read the value parameters of a remote XML node.
Parameters:
Str_xmlUrl: Remote XML address, for example, http: // 192.168.1.19/test/xml. xml
Str_dirPath: Path of the node to be searched, for example, XMLDocument/test [0]/newName2/childs [2]
JS Code:
Reference content is as follows: <Script type = "text/javascript"> Function getXMLNode (str_xmlUrl, str_dirPath) { If (! Str_xmlUrl |! Str_dirPath) Return false;
Var _ bool_IE = (window. ActiveXObject? True: false); // obtain the browser type
// Initialize 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 need to perform any operations before the XML file is read, Disable asynchronous processing. */ _ Obj_xmlDoc.async = false; // Read XML file content Try { _ Obj_xmlDoc.load (str_xmlUrl ); } // Try Catch (E) { // Read failed Return false; } // Catch /* Parse XML file content by path */ /* ILA Firefox */ If (! _ Bool_IE) { /* Parse and verify the DOM path The path must use "/" as the Separator "[N]" can be used in the path to indicate the index of the subnode. For example, "XMLDocument/item [1]" indicates the second item node under the first XMLDocument node under the root node If this method is not used, the first node is used by default. */ Var _ arr_dirPath = str_dirPath.split ('/'); If (! _ Arr_dirPath [0]) _ Arr_dirPath.shift (); If (! _ Arr_dirPath) Return false;
Var _ obj_aimElement = _ obj_xmlDoc; // Define the regular expression object used to obtain the node Index Var _ obj_reg =/\ [([0-9] +) \]/ig; /* Retrieve Nodes Based on DOM path parsing */ For (var _ int_ I = 0; _ int_ I <_ arr_dirPath.length; _ int_ I ++) { // The index of the currently qualified Node Var _ int_localIndex = 0; // Specify the node index in the DOM path Var _ int_aimIndex = 0; // Obtain the relative path of the node to be parsed Var _ str_dirPath = _ arr_dirPath [_ int_ I]; // Obtain the index of the node to be parsed Var _ arr_result = _obj_reg.exe c (_ str_dirPath ); // Search for the ID. If the ID is false, the specified node is not obtained. Var bool_catch = false; // If the node index number is specified, sort the node relative path and node index. If (_ arr_result) { _ Int_aimIndex = _ arr_result [1]; _ Str_dirPath = _ str_dirPath.replace (_ arr_result [0], ''); } // If
// Obtain all the subnodes of the current node Var _ arr_childNodes = _ obj_aimElement.childNodes; // Query the "relative path" and node index number matching the current node in the subnode of the current node For (var _ int_j = 0; _ int_j <_ arr_childNodes.length; _ int_j ++) { // Match the child node with the path If (_ arr_childNodes [_ int_j]. nodeName = _ str_dirPath) { // Obtain the node if the index matches the path // Otherwise, add "1" to the currently qualified node Index" If (_ int_localIndex = _ int_aimIndex) { _ Obj_aimElement = _ arr_childNodes [_ int_j]; Bool_catch = true; Break; } // If Else _ Int_localIndex + = 1; } // If } // // If the specified node is not obtained, an error is returned. If (! Bool_catch) { Return false; } } // // Return the searched node content Return (_ obj_aimElement.childNodes [0]. nodeValue ); } // If
/* Microsoft IE */ Try { // Return the searched node content 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:
Reference content is as follows: <? 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> |