Compartilhar via


IXMLDOMParseError

 

Returns detailed information about the last parse error, including the error number, line number, character position, and a text description.

JScript 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.6.0");  
xmlDoc.async = false;  
xmlDoc.load("books.xml");  
if (xmlDoc.parseError.errorCode != 0) {  
  WScript.Echo("A parse error occurred.");  
} else {  
  WScript.Echo(xmlDoc.documentElement.xml);  
}  
  

C/C++ Example

The following C/C++ example reads a document with a missing end "TITLE" tag (in the second book) and displays the error.

#include “msxml6.h”  
  
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.6.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();  
}  
  

XML Document

The example uses the following XML 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</TITLE>  
    <AUTHOR>Eva Corets</AUTHOR>  
    <PUBLISHER>Lucerne Publishing</PUBLISHER>  
  </BOOK>  
  <BOOK>  
    <TITLE>Splish Splash</TITLE>  
    <AUTHOR>Paula Thurman</AUTHOR>  
    <PUBLISHER>Scootney</PUBLISHER>  
  </BOOK>  
</COLLECTION>  
  

The example produces the following output.

Error:

Code = 0xc00ce56d

Source = Line : 14; Char : 5

Error Description = End tag 'BOOK' does not match the start tag 'TITLE'.

Requirements

Implementation:

msxml3.dll, msxml2.lib (MSXML 3.0)

msxml6.dll, msxml6.lib (MSXML 6.0)

Header and IDL files:

msxml2.h, msxml2.idl, msxml6.h, msxml6.idl

Versioning

Implemented in:

MSXML 3.0, MSXML 6.0

See Also

IXMLDOMParseError Members