In the previous article, both IE and FireFox have an onload () method to load XML files. When loading XML strings, IE has an onloadXML () method, while FireFox does not, therefore, to parse XML strings into DOM objects in FireFox, The DOMParser object must be used.
DomParser = new DOMParser ();
XmlDoc = domParser. parseFromString (xmlString, 'text/xml ');
The following two parameters are explained: the first is the content of the xml string, and the second is the content type of the XML tag parameter contentType text to be parsed.
Below is a small example to illustrate
Code
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE> New Document </TITLE>
<Meta name = "Generator" CONTENT = "EditPlus">
<Meta name = "Author" CONTENT = "">
<Meta name = "Keywords" CONTENT = "">
<Meta name = "Description" CONTENT = "">
<Script type = 'text/javascript '>
LoadXML = function (xmlString ){
Var xmlDoc = null;
// Determine the browser type
// Supports Internet Explorer
If (! Window. DOMParser & window. ActiveXObject) {// window. DOMParser determines whether the browser is a non-IE browser.
Var xmlDomVersions = ['msxml. 2. DOMDocument.6.0 ', 'msxml. 2. DOMDocument.3.0', 'Microsoft. XMLDOM '];
For (var I = 0; I <xmlDomVersions. length; I ++ ){
Try {
XmlDoc = new ActiveXObject (xmlDomVersions [I]);
XmlDoc. async = false;
XmlDoc. loadXML (xmlString); // The loadXML method loads xml strings.
Break;
} Catch (e ){
}
}
}
// Supports Mozilla browsers
Else if (window. DOMParser & document. implementation & document. implementation. createDocument ){
Try {
/* The DOMParser object parses the XML text and returns an XML Document object.
* To use DOMParser, instantiate it using a constructor without parameters, and then call its parseFromString () method.
* ParseFromString (text, contentType) parameter text: content type of the XML tag parameter contentType text to be parsed
* It may be one of "text/xml", "application/xml", or "application/xhtml + xml. Note: "text/html" is not supported ".
*/
DomParser = new DOMParser ();
XmlDoc = domParser. parseFromString (xmlString, 'text/xml ');
} Catch (e ){
}
}
Else {
Return null;
}
Return xmlDoc;
}
// Judge the xml Object
CheckXMLDocObj = function (xmlString ){
Var xmlDoc = loadXML (xmlString );
If (xmlDoc = null ){
Alert ('your browser does not support reading xml files, so this page prohibits your operations. We recommend you use IE5.0 or above to solve this problem! ');
// Window. location. href = 'address (for example, homepage )';
}
Return xmlDoc;
}
// Parameter 1: Control ID, parameter 2: understood as the positioning Node
InitializeSelect = function (oid, xPath ){
Var xmlstr = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> <DongFang> <Company> <cNname> 1 </cNname> <cIP> 1 </cIP> </Company> <cNname> 2 </cNname> <cIP> 2 </cIP> </Company> <cNname> 3 </cNname> <cIP> 3 </cIP> </Company> <cNname> 4 </cNname> <cIP> 4 </cIP> </Company> <cNname> 5 </cNname> <cIP> 5 </cIP> </Company> <Company> <cNname> 6 </cNname> <cIP> 6 </cIP> </Company> </DongFang> ";
Var xmlDoc = checkXMLDocObj (xmlstr );
Var n;
Var l;
Var e = document. getElementById (oid); // get the drop-down list box Control
If (e! = Null ){
N = xmlDoc. getElementsByTagName (xPath );
L = n. length;
// Add list subitems cyclically
For (var I = 0; I <l; I ++ ){
E. options. add (new Option (n [I]. getElementsByTagName ("cNname") [0]. firstChild. nodeValue, n [I]. getElementsByTagName ("cIP") [0]. firstChild. nodeValue ));
}
}
}
</Script>
</HEAD>
<BODY onload = "initializeSelect ('company', 'company')">
<Select id = 'company' name = 'company' onChange = 'showvalue () '>
<Option selected = 'selected'> </option>
</Select>
</BODY>
</HTML>