You are here: Reference > JavaScript > client-side > xml handling > objects > XMLDOMParseError

XMLDOMParseError object

Browser support:
Contains detailed information about the error of the last XML parsing.
The XMLDOMParseError object is filled by the load and loadXML methods of the XMLDocument object.

Syntax:

Properties that reference the object:
XMLDocument.parseError

Possible members:

Properties:
errorCode
Returns an integer that specifies the code of the last error.

This property is read-only.
filepos
Returns an integer that specifies the absolute position of the character where the error occurred.

This property is read-only.
line
Returns an integer that specifies the number of the line where the error occurred.

This property is read-only.
linepos
Returns an integer that specifies the position of the character within the line where the error occurred.

This property is read-only.
reason
Returns a string that contains detailed information about the error.

This property is read-only.
srcText
Returns a string that contains the contents of the line where the error occurred.

This property is read-only.
url
Returns a string that specifies the URL of the document that generated the error of the last XML parsing.

This property is read-only.

Example HTML code 1:

This example illustrates the use of the XMLDOMParseError object and the loadXML method:
<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>&lt;root&gt;&lt;fruit color='red'&gt;&lt;/root&gt;</span><br />
    <button onclick="TestContent1 ()">Build XML</button>
    <br /><br />
    <span>&lt;root&gt;&lt;fruit color='red'&gt;&lt;/fruit&gt;&lt;/root&gt;</span><br />
    <button onclick="TestContent2 ()">Build XML</button>

    <div id="info"></div>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content