You are here: Reference > JavaScript > client-side > xml handling > methods > parseFromString (DOMParser)
parseFromString method (DOMParser)
9 | ||||
Builds an XMLDocument object from the specified string.
Note: The DOMParser object and its parseFromString method are supported in Internet Explorer from version 9.
The parseFromString 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 parseFromString method to build an XMLDocument object 'manually'.
In older Internet Explorer versions (and optionally in newer ones as well), use the loadXML method for similar functionality.
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:
Required. String that specifies the contents of the XML file to parse. | |||||||||||||||||||||||||||||||
Required. String that specifies the content type of the string to parse.
Only the XML format is supported, so use one of the following equivalent values:
|
Return value:
Returns the created XMLDocument object. If the input string is not well-formed, the parseFromString method raises an exception in Internet Explorer.
Example HTML code 1:
This example illustrates the use of the parseFromString and the loadXML 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