loadXML method (XMLDocument)
Builds an XMLDocument object from the specified string.
The loadXML method resets the XMLDocument object first (clears the document represented by the XMLDocument object, resets the XMLDOMParseError property, etc.) and starts to load the specified text.
This method is useful if you have an XML formatted text and you need to handle it as an XML document.
For example, when an XML document is downloaded with the XMLHttpRequest object, the responseXML property of the XMLHttpRequest object refers to an XMLDocument object that represents the downloaded content.
If the downloaded content is not well-formed or the character encoding was detected wrong, the responseXML property refers to an XMLDocument object that describes the error.
In that case, you can use the responseText property of the XMLHttpRequest object and the loadXML method to build an XMLDocument object 'manually'.
In Firefox, Opera, Google Chrome, Safari and Internet Explorer from version 9, the parseFromString method provides similar functionality to the loadXML method.
See the example and the ParseHTTPResponse method in the attached ajax.js file of the example below for details.Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
String that specifies the contents of the XML file. |
Return value:
Boolean. One of the following values:
The loading failed. Use the XMLDOMParseError property to get information about the error. The documentElement property is set to null in that case. | |
The loading was successful. |
Example HTML code 1:
This example illustrates the use of the loadXML and the parseFromString methods:
|
||||
<head> <script type="text/javascript"> function CreateMSXMLDocumentObject () { if (typeof (ActiveXObject) != "undefined") { var progIDs = [ "Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument" ]; for (var i = 0; i < progIDs.length; i++) { try { return new ActiveXObject(progIDs[i]); } catch(e) {}; } } return null; } function BuildXMLFromString (text) { var message = ""; if (window.DOMParser) { // all browsers, except IE before version 9 var parser = new DOMParser(); try { xmlDoc = parser.parseFromString (text, "text/xml"); } catch (e) { // if text is not well-formed, // it raises an exception in IE from version 9 alert ("XML parsing error."); return false; }; } else { // Internet Explorer before version 9 xmlDoc = CreateMSXMLDocumentObject (); if (!xmlDoc) { alert ("Cannot create XMLDocument object"); return false; } xmlDoc.loadXML (text); } var errorMsg = null; if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) { errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason + " at line " + xmlDoc.parseError.line + " at position " + xmlDoc.parseError.linepos; } else { if (xmlDoc.documentElement) { if (xmlDoc.documentElement.nodeName == "parsererror") { errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue; } } else { errorMsg = "XML Parsing Error!"; } } if (errorMsg) { alert (errorMsg); return false; } alert ("Parsing was successful!"); return true; } function TestContent1 () { var xmlText = "<root><fruit color='red'></root>"; BuildXMLFromString (xmlText); } function TestContent2 () { var xmlText = "<root><fruit color='red'></fruit></root>"; BuildXMLFromString (xmlText); } </script> </head> <body> <span><root><fruit color='red'></root></span><br /> <button onclick="TestContent1 ()">Build XML</button> <br /><br /> <span><root><fruit color='red'></fruit></root></span><br /> <button onclick="TestContent2 ()">Build XML</button> <div id="info"></div> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
Related pages:
External links:
User Contributed Comments