[Original] JavaScript reading XML generic classes compatible with IE and public browsers

Source: Internet
Author: User

XMLDOM can well process XML documents, but there are considerable differences between browsers such as IE and Firefox. This kind of comprehensive processing method makes it compatible with various browsers

This class is based on xml dom and only processes incompatible parts of various browsers. It cannot be fully applied to XML processing. before using it, you must understand xml dom. For more information, see the network

Method:

  • XML. load (url) loads XML files and returns XML objects.
  • XML. loadXML (XMLString) loads XML strings and returns XML objects.
  • XML. childNodes (node) gets a node subnode and returns an array of objects. // It mainly filters out the annoying empty nodes in Firefox.
  • XML. xml (node) gets the formatted text of a node and returns string // IE. xml. This method is not available in FF.
  • XML. text (node) gets a node value and returns a string // FF. This method is also unavailable.
  • XML. selectSingleNode (node, xpath) uses xpath to obtain the first matching node under the node and return the node object. // each method has a uniform method to the DOM of IE.
  • XML. selectNodes (node, xpath) uses XPath to retrieve all matching nodes under the node and return the node object set. // each method has a uniform method to the DOM of IE.
Example of linkage between provinces and municipalities <Script src = "jquery. js"> </script>
<Script src = "xml. js"> </script>
<Style type = "text/css">
Select {width: 150px}
</Style>
<Script type = "text/javascript">
<! --
$ (Function (){

Var xml = XML. load ("city1.xml ");
Var xmldoc=xml.doc umentElement; // root layer
Var nodes = XML. selectNodes (xmldoc, "// Item [@ pid = '0']"); // retrieves the xpath Node
For (var I = 0; I <nodes. length; I ++)
$ ("<Option/> "). val (nodes [I]. getattribute ("ID ")). text (nodes [I]. getattribute ("value ")). appendto ($ ("select [name = 'addr1']");
$ ("Select [name = 'addr1']"). Change (function (){
// Link the second layer
Select_change (this. Value, 'addr2 ');
$ ("Select [name = 'addr2']"). Change ();
});
$ ("Select [name = 'addr2']"). Change (function (){
// Link to Layer 3
Select_change (this. value, "addr3 ");
});
$ ("Select [name = 'addr1']"). Change ();
$ ("Select [name = 'addr2']"). Change ();
Function select_change (value, O ){
$ ("Select [name = '" + O + "']"). Empty ();
Var nodes = XML. selectNodes (xmldoc, "// Item [@ pid = '" + value + "']");
For (var I = 0; I <nodes. length; I ++)
$ ("<Option/> "). val (nodes [I]. getAttribute ("id ")). text (nodes [I]. getAttribute ("value ")). appendTo ($ ("select [name = '" + o + "']");
 
};
});
// -->
</Script>
</Head>
<Body>
<Select name = "addr1"> </select>
<Select name = "addr2"> </select>
<Select name = "addr3"> </select>

 

Download point here

Click here for demonstration

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.