IXMLDOMParseError
Returns detailed information about the last parse error, including the error number, line number, character position, and a text description.
Example
The following script example attempts to load an XML document. It then tests the errorCode
property of the IXMLDOMParseError
object to see if an error has occurred.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0"); xmlDoc.async = false; xmlDoc.load("books.xml"); if (xmlDoc.parseError.errorCode != 0) { alert("A parse error occurred."); } else { alert(xmlDoc.documentElement.xml); }
Example
The following Microsoft® Visual Basic® example attempts to load an XML document. It then tests the errorCode
property of the IXMLDOMParseError
object to see if an error has occurred.
Dim xmlDoc As New Msxml2.DOMDocument50 xmlDoc.async = False xmlDoc.Load ("books.xml") If xmlDoc.parseError.errorCode <> 0 Then MsgBox "A parse error occurred." Else MsgBox xmlDoc.documentElement.xml End If
Example
The following C/C++ example reads a document with a missing end "TITLE" tag (in the second book) and displays the error.
#import "msxml5.dll" using namespace MSXML2; inline void TESTHR( HRESULT _hr ) { if FAILED(_hr) _com_issue_error(_hr); } void XMLDOMParserSample() { try { IXMLDOMDocumentPtr docPtr; //init TESTHR(CoInitialize(NULL)); TESTHR(docPtr.CreateInstance("Msxml2.DOMDocument.5.0")); // load a document _variant_t varXml("D:\\xmlSample\\xmlTest\\book.xml"); _variant_t varOut((bool)TRUE); varOut = docPtr->load(varXml); if ((bool)varOut == FALSE) { IXMLDOMParseErrorPtr errPtr = docPtr->GetparseError(); _bstr_t bstrErr(errPtr->reason); printf("Error:\n"); printf("Code = 0x%x\n", errPtr->errorCode); printf("Source = Line : %ld; Char : %ld\n", errPtr->line, errPtr->linepos); printf("Error Description = %s\n", (char*)bstrErr); } else { printf("Load successful"); } } catch (_com_error &e) { printf("Error:\n"); printf("Code = %08lx\n", e.Error()); printf("Code meaning = %s\n", (char*) e.ErrorMessage()); printf("Source = %s\n", (char*) e.Source()); printf("Error Description = %s\n", (char*) e.Description()); } catch(...) { printf("Unknown error!"); } CoUninitialize(); }
Document
<?xml version='1.0'?> <COLLECTION xmlns:dt="urn:schemas-microsoft-com:datatypes"> <DATE dt:dt="datetime">1998-10-13T15:56:00</DATE> <BOOK> <TITLE>Lover Birds</TITLE> <AUTHOR>Cynthia Randall</AUTHOR> <PUBLISHER>Lucerne Publishing</PUBLISHER> </BOOK> <BOOK> <TITLE>The Sundered Grail <AUTHOR>Eva Corets</AUTHOR> <PUBLISHER>Lucerne Publishing</PUBLISHER> </BOOK> <BOOK> <TITLE>Splish Splash</TITLE> <AUTHOR>Paula Thurman</AUTHOR> <PUBLISHER>Scootney</PUBLISHER> </BOOK> </COLLECTION>
Output
Error: Code = 0xc00ce56d Source = Line : 14; Char : 5 Error Description = End tag 'BOOK' does not match the start tag 'TITLE'.
Versioning
MSXML 2.0 and later
Requirements
Implementation: msxml5.dll, msxml2.lib
Header and IDL files: msxml2.h, msxml2.idl
To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button in the upper-left corner of the page.